Python 如何在jinja模板中增加for循环上的变量?
我想做一些类似的事情: 变量p来自test.py,test.py是一个列表['a','b','c','d']Python 如何在jinja模板中增加for循环上的变量?,python,jinja2,Python,Jinja2,我想做一些类似的事情: 变量p来自test.py,test.py是一个列表['a','b','c','d'] {% for i in p %} {{variable++}} {{variable}} 结果输出为: 1 2 3 4您可以使用循环。索引: {% for i in p %} {{ loop.index }} {% endfor %} 检查一下 在较新的版本中,由于范围规则,以下内容不起作用: 正如Jeroen所说,存在范围问题:如果在循环外设置“count”,则不能在循环内修改
{% for i in p %}
{{variable++}}
{{variable}}
结果输出为:1 2 3 4您可以使用
循环。索引
:
{% for i in p %}
{{ loop.index }}
{% endfor %}
检查一下
在较新的版本中,由于范围规则,以下内容不起作用:
正如Jeroen所说,存在范围问题:如果在循环外设置“count”,则不能在循环内修改它 您可以通过使用对象而不是标量来表示“count”来克服此行为:
{% set count = [1] %}
现在,您可以在forloop甚至%include%中操作count。下面是我递增计数的方式(是的,这很麻烦,但很好):
或
{%set count=[]%}
{%表示循环%}
{%set\uu=count.append(1)%}
我爱你,我爱你。。。
{{count | length}}
{%endfor%}
(来自@eyettea和@PYB的评论)来寻找Django的方法,并找到了这篇文章。也许有人需要django的解决方案
{% for item in item_list %}
{{ forloop.counter }} {# starting index 1 #}
{{ forloop.counter0 }} {# starting index 0 #}
{# do your stuff #}
{% endfor %}
请在此处阅读更多信息:
以下是我的解决方案:
将所有计数器放入字典:
{% set counter = {
'counter1': 0,
'counter2': 0,
'etc': 0,
} %}
{% macro increment(dct, key, inc=1)%}
{% if dct.update({key: dct[key] + inc}) %} {% endif %}
{% endmacro %}
{{ increment(counter, 'counter1') }}
定义宏以轻松增加它们:
{% set counter = {
'counter1': 0,
'counter2': 0,
'etc': 0,
} %}
{% macro increment(dct, key, inc=1)%}
{% if dct.update({key: dct[key] + inc}) %} {% endif %}
{% endmacro %}
{{ increment(counter, 'counter1') }}
现在,每当您想增加“counter1”计数器时,只需执行以下操作:
{% set counter = {
'counter1': 0,
'counter2': 0,
'etc': 0,
} %}
{% macro increment(dct, key, inc=1)%}
{% if dct.update({key: dct[key] + inc}) %} {% endif %}
{% endmacro %}
{{ increment(counter, 'counter1') }}
我也在与这种行为作斗争。我想根据计数器更改jinja中的div类。我很惊讶,蟒蛇式的方法不起作用。下面的代码在每次迭代时重置我的计数器,所以我只有red类
{% if sloupec3: %}
{% set counter = 1 %}
{% for row in sloupec3: %}
{% if counter == 3 %}
{% set counter = 1 %}
{% endif %}
{% if counter == 1: %}
<div class="red"> some red div </div>
{% endif %}
{% if counter == 2: %}
<div class="gray"> some gray div </div>
{% endif %}
{% set counter = counter + 1 %}
{% endfor %}
{% endif %}
{%if sloupec3:%}
{%设置计数器=1%}
{sloupec3中的行的百分比:%}
{%如果计数器==3%}
{%设置计数器=1%}
{%endif%}
{%如果计数器==1:%}
一些红色的div
{%endif%}
{%如果计数器==2:%}
一些灰色的div
{%endif%}
{%set counter=计数器+1%}
{%endfor%}
{%endif%}
我像这样使用loop.index,它可以工作:
{% if sloupec3: %}
{% for row in sloupec3: %}
{% if loop.index % 2 == 1: %}
<div class="red"> some red div </div>
{% endif %}
{% if loop.index % 2 == 0: %}
<div class="gray"> some gray div </div>
{% endif %}
{% endfor %}
{% endif %}
{%if sloupec3:%}
{sloupec3中的行的百分比:%}
{%if loop.index%2==1:%}
一些红色的div
{%endif%}
{%if loop.index%2==0:%}
一些灰色的div
{%endif%}
{%endfor%}
{%endif%}
在2.10之后,要解决范围问题,可以执行以下操作:
{% set count = namespace(value=0) %}
{% for i in p %}
{{ count.value }}
{% set count.value = count.value + 1 %}
{% endfor %}
如果有人想在循环中添加一个值,那么您可以使用它100%工作
{% set ftotal= {'total': 0} %}
{%- for pe in payment_entry -%}
{% if ftotal.update({'total': ftotal.total + 5}) %}{% endif %}
{%- endfor -%}
{{ftotal.total}}
输出=5Peter Hollingsworth。非常感谢您提供的信息,这对我帮助很大。另一个更简洁的解决方案是初始化一个空列表
{%set count=[]%}
,在每个循环中向列表中添加一个项{%set\uuuu=index.append(1)%}
,并使用长度显示索引索引|长度
。此外,您还可以使用类似这样的语句{%do index.append(1)%}
但是您必须添加所描述的扩展来纠正@eyettea的干净解决方案:另一个稍微干净一点的解决方案是初始化一个空列表{%set count=[]%}
,在每个循环中向列表添加一个项{%set\uUUUU=count.append(1)%}
并使用长度显示计数count | length
这是Django模板引擎,OP要求提供Jinja2tanks,这对我很有帮助!一个混乱问题的优雅解决方案。谢谢!非常好!我需要一个跨越两个嵌套for循环的累积计数索引。这对于该用例非常有效。使用答案解决范围问题issueI获取错误:第145行的/listing/Invalid block标记处出现TemplateSyntaxeError:'set',应为'endblock'。是否忘记注册或加载此标记?在:{%set count=namespace(value=0)%}知道原因吗?