如何在django';s模板?
我正在重建我的所有模板,使其变得更易于管理和自定义,树将如下所示:如何在django';s模板?,django,django-templates,Django,Django Templates,我正在重建我的所有模板,使其变得更易于管理和自定义,树将如下所示: ├───core │ └───templates │ └───base.html (original, touches every page) │ ... │ ├───app1 │ └───templates │ ├───base.html (extends from core/base.html, only touches app1) │ └───file1.html (e
├───core
│ └───templates
│ └───base.html (original, touches every page)
│ ...
│
├───app1
│ └───templates
│ ├───base.html (extends from core/base.html, only touches app1)
│ └───file1.html (extends from app1/base.html)
│ ...
│
└───app2
└───templates
├───base.html (extends from core/base.html, only touches app2)
└───file2.html (extends from app2/base.html)
...
要将app1
的模板连接到core
的模板,我使用{%extends'../../core/templates/base.html%}
,在core应用程序中会有{%block container%}{%endblock%}
作为示例,在app1、app2中会有其他{%block other\content%}{%endblock%}
块。正如您所看到的,块也是嵌套的
问题是我遇到了以下错误:
相对路径“../../core/templates/base.html”指向模板“base.html”所在的文件层次结构之外
问题:我想知道在不硬编码的情况下解决此问题的最佳方法是什么 原来的
base.html
与其他同类有冲突,因此我将app1
,app2
的base重命名为base[app_name].html,解决了我的问题。这棵树看起来像这样:
├───core
│ └───templates
│ └───base.html (original, touches every page)
│ ...
│
├───app1
│ └───templates
│ ├───base_app1.html (extends from core/base.html, only touches app1)
│ └───file1.html (extends from app1/base_app1.html)
│ ...
│
└───app2
└───templates
├───base_app2.html (extends from core/base.html, only touches app2)
└───file2.html (extends from app2/base_app2.html)
...
无需硬编码路径:{%extends'base.html%}
就足够了,对于应用程序模板中的文件,{%extends'base\[app\u name].html%}