【发布时间】:2019-08-07 05:07:15
【问题描述】:
我想在 Django 模板中声明一个变量并将其加一。
我的代码是
{% with 0 as my_variable %}
{% for address in user.addresses %}
{{my_variable=my_variable+1}}
{% if my_variable==1 %}
// do something
{% else %}
// do something
{% endif %}
{% endfor %}
{% endwith %}
它给出了错误
jinja2.exceptions.TemplateSyntaxError: can't assign to 'const'
如何声明变量并增加变量?
【问题讨论】:
-
你应该使用
forloop.counter,它支持0和1的起始索引。