我有 10 个 html 文件,名称分别为 1.html、2.html ..etc
我想要的是根据一个变量,某个文件应该包含在模板中。
e.g.
{% if foo.paid %}
{% include "foo/customization/{{ foo.id }}.html" %}
{% endif %}
这可能吗 ?因为在包含标签起作用之前, foo.id 没有被翻译。结果它给出了一个错误。如何以不同的方式处理这个问题?
我应该为此创建一个自定义模板标签吗?
你可以这样做添加过滤器 https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#add and 有声明 https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#with .
{% if foo.paid %}
{% with template_name=foo.id|stringformat:"s"|add:".html" %}
{% include "foo/customization/"|add:template_name %}
{% endwith %}
{% endif %}
首先你建立一个template_name
,其中包括foo.id
以字符串格式连接.html
。然后你把它传递给include
标签,与模板目录的路径连接。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)