Django:动态构造{% include %} 标签的值?

标签 django

我想使用 {% 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/

相关文章:

django - 在 Django 1.7 中运行单元测试时禁用迁移

json - 结合 DjangoObjectType 和 ObjectType - Django, GraphQL

javascript - 添加jquery后Html提交按钮不起作用

python - Django 对象是 "non subscriptable"导致我编写冗余代码

django - 检索访问 token : b'{\n "error": "redirect_uri_mismatch", 时出错\n "error_description": "Bad Request"\n}'

python - 如何在 Django 中更新用户配置文件

Django导入错误: cannot import name

django - 尝试使用Django中的套接字创建聊天应用程序

django - Gunicorn 同步 worker 生成进程

python - 我如何从 Python 中检测到 MySQL 服务器的总死亡?