调用 Python 的模板
Template from which Python was called
有没有办法传递或检测执行 Python (Flask) 函数的模板的名称?
例如,我的基本模板 base.html
中有一个表单,可以在任何其他加载的模板中提交。例如,当我在 template_x.html
中提交该表单并在 Python 中处理它时,我需要呈现另一个模板。因为我不知道表单是从哪个模板提交的,所以我需要以某种方式检测或将其 ID 传递给 Python,以便我可以呈现 template_x.html
.
您可以使用 this 技巧。在您的 base.html
表单中,添加一个隐藏元素:
<input type="hidden" name="current_template" value="{{ current_template }}" />
在扩展 base.html
的 template_x.html
中,只需添加此行(在任何块之外):
{% set current_template = 'template_x.html' %}
之所以有效,是因为子模板中块外的分配是全局的,并且在评估布局模板之前执行。
有没有办法传递或检测执行 Python (Flask) 函数的模板的名称?
例如,我的基本模板 base.html
中有一个表单,可以在任何其他加载的模板中提交。例如,当我在 template_x.html
中提交该表单并在 Python 中处理它时,我需要呈现另一个模板。因为我不知道表单是从哪个模板提交的,所以我需要以某种方式检测或将其 ID 传递给 Python,以便我可以呈现 template_x.html
.
您可以使用 this 技巧。在您的 base.html
表单中,添加一个隐藏元素:
<input type="hidden" name="current_template" value="{{ current_template }}" />
在扩展 base.html
的 template_x.html
中,只需添加此行(在任何块之外):
{% set current_template = 'template_x.html' %}
之所以有效,是因为子模板中块外的分配是全局的,并且在评估布局模板之前执行。