概要
- Djangoのテンプレートファイルは、テンプレートディレクトリーやそのサブディレクトリーに置かれることを想定している
- 複数場所に配置されたテンプレートディレクトリー以下が一括して探索される
- 探索場所と順序は
settings.pyファイルのTEMPLATESの設定に従うDIRSでは任意の場所・名前でテンプレートディレクトリーを追加できるAPP_DIRSがTrueに設定されている場合、各アプリケーションディレクトリー下のtemplatesディレクトリーが探索される
- 探索されたテンプレートを参照する場合、テンプレートディレクトリーからの相対パスで指定する
- テンプレートディレクトリーを分散配置しても一括して検索されるため、各テンプレートディレクトリーに適切なサブディレクトリーを置いて、その下にテンプレートファイルを置くのが一般的
確認用プロジェクト
確認のためtemplatetestプロジェクトを準備し、app1、app2の2つのアプリケーションを作成する。templatesディレクトリーとそのサブディレクトリーを以下のように作成し、4つの場所に即した内容のindex.htmlファイルを置く。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
templatetest/ ├── app1 │ ├── templates │ │ └── app1 │ │ └── index.html │ ├── app2 │ ├── templates │ │ └── app2 │ │ └── index.html │ ├── config │ ├── templates │ │ └── subdirectory │ │ └── index.html │ └── templates └── base └── index.html |
各場所のindex.htmlは、title要素/bodyのp要素で以下を表示させるようにした。
- App1/Under the app1 directory
- App2/Under the app2 directory
- Subirectory/Under the subdirectory
- Base Directory/Under the base directory
そしてapp1のビューファイルで指定するテンプレート名のディレクトリー名を変更しながら、各場所のindex.htmlが読み込まれたかどうか確認する。
|
1 2 3 4 5 6 |
from django.shortcuts import render # Create your views here. def index(request): template = 'base/index.html' return render(request, template) |
プロジェクトの完成後、template変数に設定した文字列を以下のように変更して、それぞれに対応したページが表示されるかどうかを確認する。
'base/index.html''subdirectory/index.html''app1/index.html''app2/index.html'
TEMPLATESの初期設定
初期状態でのTEMPLATESは以下の通り。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], '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', ], }, }, ] |
このうちテンプレートの探索場所は'DIRS'と'APP_DIRS'で設定される。
なおディレクトリーの探索場所が設定されていれば、そのサブディレクトリー下も再帰的に探索される。
DIRSでの設定
プロジェクトディレクトリー直下
'DIRS'にはプロジェクト内任意の場所の、任意の名前のディレクトリーを設定できる。たとえばプロジェクトディレクトリー直下に置いたtemplatesを探索させるなら以下のように記述する。
|
1 |
'DIRS': ['templates'], |
または
|
1 2 3 |
import os .... 'DIRS': [os.path.join(BASE_DIR, 'templates')], |
後者の場合、settings.pyではimport osがないので追加が必要。
この設定により、'templatetest/templates/base/index.html'のレンダリングが可能になる。
サブディレクトリー下
プロジェクトの設定ファイル群があるサブディレクトリー下(この場合configディレクトリー)にあるディレクトリーを探索させるときは以下のように相対パスで設定する。
|
1 |
'DIRS': ['config/templates'], |
または
|
1 2 3 |
import os .... 'DIRS': [os.path.join(BASE_DIR, 'config/templates')], |
この場合もsettings.pyにimport osが必要。
この設定により、templatetest/config/subdirectory/templates/index.htmlのレンダリングが可能になる。
APP_DIRSの設定
また、'APP_DIRS'がTrueに設定されている場合は、各アプリケーションディレクトリー下にあるtemplatesディレクトリーが探索される。
デフォルトでTrueがセットされているので、アプリケーションディレクトリー下にtempratesディレクトリーがあれば全て探索される。
この例の場合、app1/templates、app2/templatesは、デフォルトの設定)のままでそれぞれのサブディレクトリーも含めて探索される。
この結果templatetest/app1/templates/app1/index.htmlとtemplatetest/app2/templates/app2/index.htmlのレンダリングが可能になる。
全てを探索させるTEMPLATESの設定
これまでの4つの場所のすべてを探索対象とするには、settings.pyのTEMPLATESを以下のように設定する。
|
1 2 3 4 5 6 7 8 |
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': ['templates', 'config/templates'], 'APP_DIRS': True, ........ }, ] |
この結果、以下の4つが探索可能になる。
templatetest/templates/base/index.htmltemplatetest/config/templates/subdirectory/index.htmltemplatetest/app1/templates/app1/index.htmltemplatetest/app2/templates/app2/index.html
templatesのサブディレクトリー
特にアプリケーションディレクトリー下にテンプレートを置く場合に、(index.htmlのように)同じ名前のファイルが重複しないよう、サブディレクトリーを設けるのが一般的。
通常、サブディレクトリー名はアプリケーションディレクトリー名と同じとする。冗長だが重複が防止され、可読性も保たれる。
|
1 2 3 4 5 |
project/ ├── application │ ├── templates │ │ └── application │ │ └── index.html |