我的 Django 版本是 1.6.0 的,看的学习资料是The Django Book, 在学习模板的这一章时,资料上讲的 "TEMPLATE_DIRS" 的这个选项在 settings 这个文件里没有找到
后来在网上找到说是 1.5 和 1.6 在这点上不一样,1.6 默认是不需要设置的,默认的寻找路径时在 app 下面新建的一个 templates 目录(必须完全一致),把模板放进去,同时别忘记在settings.py 里面的INSTALLED_APPS 配置项下加上你的app。而后系统会自己找到模板。
注意:1.6 的模板默认行为是针对 app 下的,并不是所创建项目下的!
Template-loader postmortem
Django tried loading these templates, in this order:
- Using loader
django.template.loaders.filesystem.Loader
:D:\MyPython\Template\src\Template\Template\templates\time2.html
(File does not exist)
- Using loader
django.template.loaders.app_directories.Loader
:C:\Python27\lib\site-packages\django\contrib\admin\templates\time2.html
(File does not exist)C:\Python27\lib\site-packages\django\contrib\auth\templates\time2.html
(File does not exist)D:\MyPython\Template\src\Template\app\templates\time2.html
(File does not exist)D:\MyPython\Template\src\Template\app2\templates\time2.html
(File does not exist)
app_directories.Loader是根据settings.py 里面的INSTALLED_APPS 配置项去搜索
默认情况下,Django 是先使用filesystem.Loader 的,只有当 filesystem.Loader 找不到模板时,才会使用 app_directories.Loader。
默认的settings.py里的TEMPLATE_DIRS设置为:
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]