I have a blueprint with its own template folder. There is also a base layout template in the "top" templates folder. I want to access this template from a blueprint template. I tried the following but got a "Template Not Found" error.
我有一个蓝图,有自己的模板文件夹。 “顶部”模板文件夹中还有一个基本布局模板。我想从蓝图模板访问此模板。我尝试了以下但得到了“找不到模板”错误。
{% extends "../../../../../templates/layout.j2" %}
The package looks like this:
包看起来像这样:
__init__.py
├── modules
│ └── citrixlb
│ ├── citrixlb.py
│ └── templates
└── templates
└── layout.j2
I set up the blueprint's template_folder
:
我设置了蓝图的template_folder:
citrix = Blueprint('citrix', __name__, template_folder='templates', url_prefix='/citrix')
What is the correct way to reference a template from another template directory?
从另一个模板目录引用模板的正确方法是什么?
1 个解决方案
#1
Flask's Jinja loader flattens the global folder and all blueprint folders into a common tree, prioritizing the global folder. So simply refer to the template as if it was at the top level location.
Flask的Jinja加载器将全局文件夹和所有蓝图文件夹展平为一个公共树,优先考虑全局文件夹。因此,只需将模板称为顶层位置即可。
{% extends "layout.j2" %}
Note that by using the "j2" extension rather than ".html", you've probably inadvertently disabled automatic escaping. It is recommended that you use the ".html" extension for html templates.
请注意,通过使用“j2”扩展名而不是“.html”,您可能无意中禁用了自动转义。建议您对html模板使用“.html”扩展名。
#1
Flask's Jinja loader flattens the global folder and all blueprint folders into a common tree, prioritizing the global folder. So simply refer to the template as if it was at the top level location.
Flask的Jinja加载器将全局文件夹和所有蓝图文件夹展平为一个公共树,优先考虑全局文件夹。因此,只需将模板称为顶层位置即可。
{% extends "layout.j2" %}
Note that by using the "j2" extension rather than ".html", you've probably inadvertently disabled automatic escaping. It is recommended that you use the ".html" extension for html templates.
请注意,通过使用“j2”扩展名而不是“.html”,您可能无意中禁用了自动转义。建议您对html模板使用“.html”扩展名。