我想使用 {% include page.html %}
在我的 Django 模板中添加标签,并构造 page.html
的值动态。
有没有办法在 Django 中做到这一点?
这是一个伪代码示例:
{% include page_{{mode}}.html %}
谢谢!
最佳答案
从文档:
The template name can either be a variable or a hard-coded (quoted) string, in either single or double quotes.
因此,在您的 View 中构造一个变量并通过上下文将其传递给您的模板,例如“modeTemplate”:“page_”+mode+“.html”。然后做:
{% include modeTemplate %}
假设 'mode' 是 View 代码中的 python 变量。
关于Django:动态构造{% include %} 标签的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4670323/