这是我的模板路径
webapp/
|__templates/
|__frontpage/
| |__home.html
|__home_base.html
在home.html中,它有:
{% extends "home_base.html" %}
此文件结构将起作用。但是,我想将home_base.html放在frontpage /中,因为这更有意义。但是,如果home_base.html被移动到frontpage /,Django将报告home_base.html模板不存在。
错误说它找不到templates /文件夹下的home_base.html文件。由于home_base.html已移至首页/,为什么不在首页/首页内搜索home_base.html?我缺少的任何配置?
答案 0 :(得分:1)
您需要执行以下操作才能扩展模板。
{% extends "frontpage/home_base.html" %}
Django不知道您移动模板的位置。它将根据您在设置中定义的模板路径查找模板。
template loader将在DIRS
设置的TEMPLATES
设置中定义的目录中查找模板。
来自DIRS
设置文档:
引擎应查找模板源文件的目录 搜索顺序。
此外,如果frontpage
是一个应用,并且您已将模板放在frontpage
应用中而不是templates
文件夹中,那么您可以将APP_DIRS
设置设置为是True
。这将告诉Django在各个应用程序中也找到模板。
来自APP_DIRS
设置文档:
引擎是否应该在里面查找模板源文件 已安装的应用程序。