关于Django1.6版本的Template设置

我的 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)
上面是模板加载出现故障的提示,我们可以清晰地看到: filesystem.Loader 找模板的方式:它会根据settings文件中设置的TEMPLATE_DIRS 去找模板。TEMPLATE_DIRS  是一个“ iterable of filesystem directories”,filesystem.Loader 根据这里指定的directories,去文件系统中寻找模板。而Django1.6它默认路径为与settings.py同目录;而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')]


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值