Python 在Django模板中声明变量并增加该变量
我想在Django模板中声明一个变量,并将其增加一个 我的代码是Python 在Django模板中声明变量并增加该变量,python,django,Python,Django,我想在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 {%
{% 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:无法分配给“const”
如何声明变量并增加它?
{%用于项目列表%中的项目]
{{forloop.counter}{{起始索引1}
{{forloop.counter0}}{#起始索引0}
{做你的事}
{%endfor%}
{{forloop.counter}}{#循环的当前迭代(1索引)#}
{{forloop.counter0}}{#循环的当前迭代(0索引)#}
还要记住
{{forloop.first}}{#如果这是第一次通过循环,则为True#
及
{{forloop.last}}{#如果这是最后一次通过循环,则为True#
{%用于项目列表%中的项目]
{{forloop.counter}{{起始索引1}
{{forloop.counter0}}{#起始索引0}
{做你的事}
{%endfor%}
{{forloop.counter}}{#循环的当前迭代(1索引)#}
{{forloop.counter0}}{#循环的当前迭代(0索引)#}
还要记住
{{forloop.first}}{#如果这是第一次通过循环,则为True#
及
{{forloop.last}}{#如果这是最后一次通过循环,则为True#
应该使用的是forloop.counter
,它同时支持起始索引0和1。应该使用的是forloop.counter
,它同时支持起始索引0和1。