以下是我需要更新变量的代码。
{% with a=1 %}
{% for x in object_list %}
{% if 'clone' in x.game and a is 1 %}
<div class="alert alert-primary">
<h2 class="p-3">{{ x.title }}</h2>
</div>
{{ a=2 }} # here is where I update it
{% endif %}
{% endfor %}
{% endwith %}
但不是 {{ a=2 }}
将 a
设置为 2,而是抛出以下错误:
TemplateSyntaxError at /
Could not parse the remainder: '=2' from 'a=2'
最佳答案
无法在 with
模板标签中重新分配变量。
您应该在后端( View )执行此操作,或者通过将变量加载到 JavaScript 中并执行您想要执行的操作,它可以在客户端执行。
关于django - 如何在 Django 模板中更新由 'with' 设置的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68200206/