Aka*_*nde 19 django django-templates
我有10个html文件,名称为1.html,2.html ..etc我想要的是根据变量,某个文件应该包含在模板中.
例如
{% if foo.paid %}
{% include "foo/customization/{{ foo.id }}.html" %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
这可能吗 ?导致在包含标记工作之前未翻译foo.id.结果它给出了一个错误.如何以不同的方式处理这个问题?我应该为此创建自定义模板标记吗?
Ser*_*ney 32
您可以使用add filter和with statement来完成.
{% if foo.paid %}
{% with template_name=foo.id|stringformat:"s"|add:".html" %}
{% include "foo/customization/"|add:template_name %}
{% endwith %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
首先你构建一个template_name,它由foo.id串联的字符串格式组成.html.然后将它传递给include标记,与模板目录的路径连接.
| 归档时间: |
|
| 查看次数: |
9457 次 |
| 最近记录: |