Article

DjangoのTemplateローダ

※ 商品リンクから購入されると少額の報酬が発生することがあります。

ueblogのスクリーンキャストに対して、 嘘の補足を行ってしまいました

嘘というのは「settings.pyのTEMPLATE_DIRSに対して設定を行わなくても、プロジェクト直下とアプリケーション直下のtemplatesディ レクトリに対してサーチパスが設定される」というものです。 正解は「自動でサーチパスが設定される場所は、settings.pyのINSTALLED_APPSに登録されている各アプリケーション直下のtemplates ディレクトリに対してのみ」です。

嘘を書いてそのままというものあれなので、動作パターンの確認を行いました。

パターン1

  • TEMPLATE_DIRSINSTALLED_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が利用される

パターン2

  • TEMPLATE_DIRSINSTALLED_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が利用される

パターン3

  • TEMPLATE_DIRSINSTALLED_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が利用される

パターン4

  • TEMPLATE_DIRSINSTALLED_APPS

  • $PROJECT_HOME/templatessample.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 が利用される

パターン5

  • TEMPLATE_DIRSINSTALLED_APPS

  • $PROJECT_HOME/templatessample.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 が利用される

パターン6

  • TEMPLATE_DIRSINSTALLED_APPS

  • $PROJECT_HOME/templates2

  • $PROJECT_HOME/templatessample.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ローダの振る舞い

優先度は以下の通りと考えられる。

  1. TEMPLATE_DIRSの最初に指定されたディレクトリを機転として指示された全てのテンプレートを探す

  2. 見つからなかったテンプレートをTEMPLATE_DIRSの次番・またその次という順序で探す

  3. 次に、INSTALLED_APPSの最初に指定されたアプリケーション直下のtemplatesディレクトリを機転として、見つからなかったテンプレートを探す

  4. 同様にINSTALLED_APPSを順に探す

どのようにテンプレートを格納すべきか

  • INSTALLED_APPSやTEMPLATE_DIRSの設定次第で選択されるテンプレートが変わらないように検討する。

  • アプリケーション固有のテンプレートAPP/xxx_xxx.xxxはAPP/templates/APPの下にのみ格納する。

  • プロジェクト全体に関わるテンプレートはTEMPLATE_DIRSに指定したディレクトリの直下に格納する。

  • アプリケーション固有のテンプレートをプロジェクトとして上書きしたい場合には、TEMPLATE_DIRSに指定したディレクトリの直下にAPP/xxx_xxx.xxxとして格納する。

Prev Entry

Next Entry