よしたく blog

ほぼ週刊で記事を書いています

Djangoのtemplatesを一つにまとめる

f:id:yoshitaku_jp:20180711100334p:plain

目次

  1. アプリケーション配下にtemplatesフォルダを作る
  2. アプリケーションを追加したときに、またtemplatesフォルダを作るか悩む…
  3. プロジェクト配下にすべてのアプリケーションのtemplatesフォルダをまとめたものを作れば解決
    1. プロジェクト配下にtemplatesフォルダを作成する
    2. setting.pyの設定を変える
  4. 余談

DjangoPythonのWebアプリケーションフレームワークです。いろいろできるので一番人気かな?他にもflaskがありますね。

Django(ジャンゴ)は、Pythonで実装されたWebアプリケーションフレームワーク。(中略)Django の第一の目的は、複雑なデータベース主体の Web サイトの構築を簡単にすることである。Djangoコンポーネントの再利用性と'pluggability'、素早い開発、DRY (Don't Repeat Yourself)の原則に力点を置いている。ファイルやデータのモデルにいたるまで、Python が一貫して用いられている。

wikipediaより

アプリケーション配下にtemplatesフォルダを作る

Djangoでは、htmlファイルが入っているtemplatesフォルダを各アプリケーションの下に配置することがあるかと思います。

例では「groups」アプリケーションの配下でtemplatesを使っています。(わかりやすくするために、必要ではないディレクトリは大幅に削除しています。

.
├── README.md
├── config
│   └── 略
├── db.sqlite3
├── groups
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   ├── models.py
│   ├── tests.py
│   ├── templates
│   │    ├── base.html
│   │    └── groups
│   │       ├── detail.html
│   │       ├── index_page.html
│   │       ├── list.html
│   │       └── regist.html
│   ├── urls.py
│   └── views.py

アプリケーションを追加したときに、またtemplatesフォルダを作るか悩む…

自分の場合は「groups」アプリケーションの他にログイン機能を使う「accounts」アプリケーションを開発途中で追加しました。このタイミングで再びtemplatesフォルダを「accounts」アプリケーションの配下に作るのが嫌でした。

.
├── README.md
├── accounts
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── config
│   └── 略
├── db.sqlite3
├── groups
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   ├── models.py
│   ├── tests.py
│   ├── templates
│   │    ├── base.html
│   │    └── groups
│   │       ├── detail.html
│   │       ├── index_page.html
│   │       ├── list.html
│   │       └── regist.html
│   ├── urls.py
│   └── views.py

プロジェクト配下にすべてのアプリケーションのtemplatesフォルダをまとめたものを作れば解決

プロジェクト配下にtemplatesフォルダを作成する

なので、プロジェクト配下にtemplatesフォルダを作成し、 「base.html」を共通化し、さらに各アプリケーションごとのフォルダを作ってオリジナリティーのある画面を作っていきます。加えて、setting.pyの中も書き換えます。このプロジェクト配下のtemplatesフォルダを見てくれよーって設定にします。

.
├── README.md
├── accounts
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── config
│   └── 略
├── db.sqlite3
├── groups
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── forms.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
└── templates
    ├── base.html
    ├── groups
    │   ├── detail.html
    │   ├── index_page.html
    │   ├── list.html
    │   └── regist.html
    └── registration
        └── login.html

setting.pyの設定を変える

setting.pyのTEMPLATESの項を探してください。

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
#        'DIRS': [], ここを
        'DIRS': [os.path.join(BASE_DIR, 'templates')], # こうする
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

以上で、Djangoのtemplatesを一つにまとめることができます。 お疲れ様でした。

余談

各アプリケーションの配下にtemplatesフォルダを作成するのにも利点があります。 対象のアプリケーションを別のプロジェクトで使いたいときです。 ときに意識せず、そのままコピーしていけます。