我如何在 Django 模板文件的一侧指定一个变量
How do i specify a variable in side of django template file
所以我试图在我的 Django 模板文件中声明一个变量
{% with object = "{{object.id
}}" %}
{% for detail in details %}
{% if detail.post.id == {{object}} %}
{{detail.name}}
{% endif %}
{% endfor %}
{% endwith %}
我知道 with 用于这项工作,但是当我 运行 这段代码时,它向我显示了这个错误:'with' expected at least one variable assignment
请帮帮我。谢谢
post_id
可能是 int
,因此您应该将 object
指定为 {% with object=2 %}
,此外,您不应在模板标签中使用双花括号:
{% with <b>object=2</b> %}
{% for detail in details %}
{% if detail<b>.post_id</b> == <b>object</b> %}
{{ detail.name }}
{% endif %}
{% endfor %}
{% endwith %}
然而,在模板中过滤不是一个好主意,因为这效率不高,并且需要更多内存,通常您在 视图.[=15= 中过滤]
所以我试图在我的 Django 模板文件中声明一个变量
{% with object = "{{object.id
}}" %}
{% for detail in details %}
{% if detail.post.id == {{object}} %}
{{detail.name}}
{% endif %}
{% endfor %}
{% endwith %}
我知道 with 用于这项工作,但是当我 运行 这段代码时,它向我显示了这个错误:'with' expected at least one variable assignment
请帮帮我。谢谢
post_id
可能是 int
,因此您应该将 object
指定为 {% with object=2 %}
,此外,您不应在模板标签中使用双花括号:
{% with <b>object=2</b> %}
{% for detail in details %}
{% if detail<b>.post_id</b> == <b>object</b> %}
{{ detail.name }}
{% endif %}
{% endfor %}
{% endwith %}
然而,在模板中过滤不是一个好主意,因为这效率不高,并且需要更多内存,通常您在 视图.[=15= 中过滤]