ueblogのスクリーンキャストに対して、嘘の補足を行ってしまいました。
嘘というのは「settings.pyのTEMPLATE_DIRSに対して設定を行わなくても、プロジェクト直下とアプリケーション直下のtemplatesディレクトリに対してサーチパスが設定される」というものです。
正解は「自動でサーチパスが設定される場所は、settings.pyのINSTALLED_APPSに登録されている各アプリケーション直下のtemplatesディレクトリに対してのみ」です。
嘘を書いてそのままというものあれなので、動作パターンの確認を行いました。
| TEMPLATE_DIRS | INSTALLED_APPS |
|---|---|
| 設定無し | sample.app1 sample.app2 |
各アプリの下にサーチパスが設定されるtemplatesディレクトリを作成した。
以下、'+'はディレクトリ、'-'はファイルを表すこととする。また、test.htmlはbase.htmlを継承したテンプレートとする。
+sample
+ app1
+ templates
+ app1
- test.html
- base.html
+ app2
+ templates
+ app2
- test.html
- base.html
・'app1/test.html'をレンダリングすると、sample/app1/templates/app1/test.htmlとsample/app1/templates/base.htmlが利用される
・'app2/test.html'をレンダリングすると、sample/app2/templates/app2/test.htmlとsample/app1/templates/base.htmlが利用される
| TEMPLATE_DIRS | INSTALLED_APPS |
|---|---|
| 設定無し | sample.app1 sample.app2 |
+sample
+ app1
+ templates
+ app1
- test.html
+ app2
- test.html
- base.html
+ app2
+ templates
+ app2
- test.html
- base.html
・'app1/test.html'をレンダリングすると、sample/app1/templates/app1/test.htmlとsample/app1/templates/base.htmlが利用される
・'app2/test.html'をレンダリングすると、sample/app1/templates/app2/test.htmlとsample/app1/templates/base.htmlが利用される
| TEMPLATE_DIRS | INSTALLED_APPS |
|---|---|
| 設定無し | sample.app2 sample.app1 |
+sample
+ app1
+ templates
+ app1
- test.html
+ app2
- test.html
- base.html
+ app2
+ templates
+ app2
- test.html
- base.html
・'app1/test.html'をレンダリングすると、sample/app1/templates/app1/test.htmlとsample/app2/templates/base.htmlが利用される
・'app2/test.html'をレンダリングすると、sample/app2/templates/app2/test.htmlとsample/app2/templates/base.htmlが利用される
| TEMPLATE_DIRS | INSTALLED_APPS |
|---|---|
| $PROJECT_HOME/templates | sample.app1 sample.app2 |
+sample
+ app1
+ templates
+ app1
- test.html
- base.html
+ app2
+ templates
+ app2
- test.html
- base.html
+ templates
- base.html
・'app1/test.html'をレンダリングすると、sample/app1/templates/app1/test.htmlとsample/templates/base.htmlが利用される
'app2/test.html'をレンダリングすると、sample/app2/templates/app2/test.htmlとsample/templates/base.htmlが利用される
| TEMPLATE_DIRS | INSTALLED_APPS |
|---|---|
| $PROJECT_HOME/templates | sample.app1 sample.app2 |
+sample
+ app1
+ templates
+ app1
- test.html
+ app2
- test.html
- base.html
+ app2
+ templates
+ app2
- test.html
- base.html
+ templates
+ app2
- test.html
- base.html
・'app1/test.html'をレンダリングすると、sample/app1/templates/app1/test.htmlとsample/templates/base.htmlが利用される
'app2/test.html'をレンダリングすると、sample/templates/app2/test.htmlとsample/templates/base.htmlが利用される
| TEMPLATE_DIRS | INSTALLED_APPS |
|---|---|
| $PROJECT_HOME/templates2 $PROJECT_HOME/templates | sample.app1 sample.app2 |
+sample
+ app1
+ templates
+ app1
- test.html
+ app2
- test.html
- base.html
+ app2
+ templates
+ app2
- test.html
- base.html
+ templates
- base.html
+ templates2
- base.html
・'app1/test.html'をレンダリングすると、sample/app1/templates/app1/test.htmlとsample/templates2/base.htmlが利用される
'app2/test.html'をレンダリングすると、sample/templates/app2/test.htmlとsample/templates2/base.htmlが利用される
・TEMPLATE_DIRSの最初に指定されたディレクトリを機転として指示された全てのテンプレートを探す
・見つからなかったテンプレートをTEMPLATE_DIRSの次番・またその次という順序で探す
・次に、INSTALLED_APPSの最初に指定されたアプリケーション直下のtemplatesディレクトリを機転として、見つからなかったテンプレートを探す
・同様にINSTALLED_APPSを順に探す
・INSTALLED_APPSやTEMPLATE_DIRSの設定次第で選択されるテンプレートが変わらないように検討する。
・アプリケーション固有のテンプレートAPP/xxx_xxx.xxxはAPP/templates/APPの下にのみ格納する。
・プロジェクト全体に関わるテンプレートはTEMPLATE_DIRSに指定したディレクトリの直下に格納する。
・アプリケーション固有のテンプレートをプロジェクトとして上書きしたい場合には、TEMPLATE_DIRSに指定したディレクトリの直下にAPP/xxx_xxx.xxxとして格納する。