四季版オペラ座の怪人とIT業界 | Top | Django勉強会 Disc1

 

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/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ローダの振る舞い

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

・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として格納する。


Taged with:,

urihttp://www.everes.net/2006/nov/22/django-template-loader/

Entry Date:2006-11-22 01:32

Author:makoto

ping url:http://www.everes.net/2006/nov/22/django-template-loader/tbping/

subscribe:feed with LDR

Add Comment

コメント追加






What's Next
Tags
Blog Archive
Project
  • » pymagnolia

    ma.gnolia.comのAPIを操作するPythonライブラリ。

  • » django-ja

    Djangoと日本の仲間たち。
    Djangoに関するニュースや、「仲間たち」のブログエントリをアグリゲートしています。

  • » rhaco

    PHPのWebフレームワーク。 PHPらしさを失わず直感的に記述できることを目指している。 Djangoと同じく、他のライブラリに依存しない。また、Djangoっぽいテンプレートの作り方も可能。

  • » Tracka

    スモールチーム、家族用の知共有ウェブアプリケーション。
    テキスト、画像、youtube動画、stage6動画等をトラック!

  • » BMO

    BMOは、本、CD、DVD、ゲームに印刷されているバーコードをiSightで読み取り、AmazonのAPIを利用して画像や情報をmacに保存します。iSightのついているmacで利用してください。Leopardと呼ばれているOSX 10.5以上が必要です。 保存した情報は、OSX Leopard(10.5)から搭載されたCover FlowとQuick Lookという仕組みを利用して表示します。