Web制作、Web開発の歩き方

初心者のためのDjango入門

■第1話:Djangoのしくみ(MTVモデル)

(最終更新日:2024.11.12)

Djangoフレームワークのイメージ
この記事は5分で読めます!
(絵が小さい場合はスマホを横に)

「日本語の文献が少ない!」

Djangoを使ってみて、まず思ったことはこれである。他のフルスタックフレームワーク(Ruby on Rails、Laravel)と比べると、 日本語の本や動画教材がとても少ないということである。 この点はDjangoを使う上でのデメリットである。ただし、ここ数年、分かりやすい本や動画がかなり出てきていて、状況は改善されつつある。 評価の高い本やUdemyなどの動画教材で学べば、早ければ3か月、遅くとも半年くらいでDjango使いになれるだろう。 Djangoは痒いとこまで手が届く、とても素晴らしいフルスタックフレームワークである。興味が沸いたのであれば、ぜひこの機会に使ってみよう。

世界的にみると、Djangoは最も良く使われているフルスタックフレームワークである。 Instagram、YoutubeもDjangoで作られている。 多くの企業で使われる理由としては、ユーザー管理機能やセキュリティ対策をはじめ、必要な機能が極めて充実していることが挙げられる。 (ただし、RailsやLaravelも充実しているのでそれらと比べれば明確なアドバンテージとは言い難いが、 それでもDjangoの長所であることは間違いない。) 加えて、Python製のため機械学習アプリケーションと相性が良い。同じ言語で前処理から分析、API作成まで行える。 この点が、他フレームワークと比較した際のアドバンテージと言える。

また、Djangoは2005年からPythonの最も代表的なフレームワークとしてその地位を譲っておらず、 今後も長く使い続けられる可能性が高い。 また、古くから開発されているので、基本的な機能は充分枯れていて大きな変更もなく、その対応でアタフタする心配も少ない。 バージョンアップの計画も公式サイトで示されている。 メンテナンスに関してもしばらくは安心である。 最近のニュースだと、Django4でPython11に対応したことや非同期処理に対応したのも魅力的だ。 今後も精力的に開発されることは間違いないだろう。

今回は、そんなフルスタックフレームワーク「Django」について、初心者の方でも分かるよう、簡単に説明していきたいと思う。 最初からたくさん説明すると理解しきれないと思うので、第1話では、DjangoのしくみとDjangoの始め方までを説明する。


1.Djangoのしくみ(MTVモデル)

Djangoの仕組みをMVCに当てはめて分析すると、以下の図のようになる。

MVCに当てはめたDjangoのしくみ

Djangoの仕組みはMCVと比較して、MTVと呼ばれている。 ④のTemplateのT、②のViews.pyのVをとってそう呼んでいる。 ただし、DjangoのViewはView(見た目)に有らず、他フレームワークのMVCでいうControllerにあたる。 そして、TemplateがViewに当たる。 まずは、この呼び方の違いを認識しよう。これらの機能を具体的に説明すると、

①のurls.pyでルーティングを行い
②のmodels.pyでデータベース(DB)とデータのやり取りを行い
③views.pyでDBから取得したデータを加工し、変数化、表示のベースとなるTemplateを指定し
④のTemplateでHTMLで表示する具体的な記述をする

ということである。よって、views.pyがModel(DB操作)とTemplate(見た目)の仲介役をしていると考えれば分かりやすい。

実際のコードの様子は以下の例のようになる。 ②のviews.pyがModelとTemplateを指定している様子が分かる。 全体像と個々のファイルの役割が掴めてしまえば、あとはいじって覚えるだけである。 やっていることは、他のフレームワークと変わらない。

Djangoのしくみに対するコード例

2.Djangoの始め方、インストール方法

ここでは、仮想環境を使わない方法で記述する。Python3系が既にインストールされているものとする。 Windowsならコマンドプロンプト、MacならTerminalで、
「pip install django」
と命令してインストールする。無事インストールできたら、
「django-admin startproject プロジェクト名」
でプロジェクトを作成できる。もし、うまくいかないようであれば、
「python -m django startproject プロジェクト名」
を実行してほしい。プロジェクトディレクトリが作成されているはずだ。 これをVSCodeで開いて、VSCode上のTerminalで
「python manage.py runserver」
と命令すれば、無事Djangoが起動するはずだ。
(python2系もインストール済だと、pip→pip3、python → python3に置き換える必要がある)
これで、自分のPCからいつでもDjangoを使うことができる。

次回から、Djangoの具体的な使い方を説明する。

Djangoが起動できたら成功

▼参考図書、サイト

 「Djangoのツボとコツがゼッタイにわかる本」 大橋亮太 秀和システム
 「現場で使えるDjango管理サイトのつくり方」 横瀬明仁


Django for Beginners ■ Episode 1: How Django Works (MTV Model) (Last Updated: 2024.11.12) Image of the Django framework You can read this article in 5 minutes! (If the image looks small, try rotating your phone) "There’s so little Japanese documentation!" That was my first impression when using Django. Compared to other full-stack frameworks like Ruby on Rails or Laravel, Japanese books and video materials are relatively scarce. This can be seen as a drawback when using Django. However, in recent years, more beginner-friendly resources have been published, and the situation is gradually improving. If you learn from highly-rated books or courses on platforms like Udemy, you could become proficient with Django in as little as three months—or six months at the latest. Django is a fantastic full-stack framework that covers all the bases. If you’re interested, now’s a great time to give it a try. Globally, Django is one of the most widely used full-stack frameworks. Platforms like Instagram and YouTube are built with Django. One major reason companies adopt Django is its robust set of built-in features, such as user management and security functions. (Although frameworks like Rails and Laravel also have rich features, Django’s strengths are still significant.) Additionally, Django is written in Python, which makes it highly compatible with machine learning applications. You can do preprocessing, analysis, and API development all in the same language. This is a clear advantage over other frameworks. Django has been the leading Python framework since 2005 and will likely continue to be widely used in the future. Its core features are stable and mature, reducing the risk of frequent, disruptive changes. The upgrade roadmap is also available on the official site, offering peace of mind for long-term maintenance. Recent updates, like support for Python 11 and asynchronous processing in Django 4, are also exciting. Django’s development shows no signs of slowing down. This article will explain what Django is in a simple way, even for beginners. To avoid overwhelming you, Episode 1 focuses on how Django works and how to get started. [Table of Contents] How Django Works (MTV Model) Getting Started with Django: Installation 1. How Django Works (MTV Model) Django’s structure can be analyzed through the lens of MVC as shown in the diagram below. Django Structure in MVC Terms Django’s structure is often referred to as MTV, rather than MVC. It takes the "T" from Template and the "V" from Views.py. However, Django’s View is not the "view" in MVC; it corresponds more closely to the Controller in MVC. Meanwhile, the Template is what you actually see—similar to MVC’s View. So first, it's important to understand this naming difference. Specifically: ① `urls.py` handles routing ② `models.py` interacts with the database (DB) ③ `views.py` fetches data from the DB, processes it, and passes it to the Template ④ The Template renders the final HTML output In short, `views.py` acts as the bridge between the Model (DB) and the Template (UI). Here's an example to illustrate this. You can see how `views.py` specifies both the Model and the Template. Once you understand the overall structure and the role of each file, the rest is just hands-on practice. The concept isn’t much different from other frameworks. Code Example: Django Structure 2. Getting Started with Django: Installation This section describes how to get started *without* using a virtual environment. Assume Python 3 is already installed. On Windows, open Command Prompt; on Mac, open Terminal. Then run: pip install django After successful installation, create a project with: django-admin startproject projectname If that doesn’t work, try: python -m django startproject projectname This will create your project directory. Open it with VSCode, and in the Terminal, run: python manage.py runserver Django should now be running. (If Python 2 is also installed, you may need to use `pip3` and `python3` instead.) You can now start using Django from your local machine anytime. In the next episode, we’ll explore how to use Django in more detail. Django running successfully ▼ Recommended Books & Resources  "Django no Tsubo to Kotsu ga Zettai ni Wakaru Hon" by Ryota Ohashi, Shuwa System (Japanese)  "Genba de Tsukaeru Django Kanri Site no Tsukurikata" by Akihito Yokose (Japanese)