2D forloop计数器django模板

时间:2014-07-10 12:41:28

标签: python django django-templates

我在django中有这样的2D forloop:

        {% for temp in data.lines.all %}
            {% for item in temp.articles.all %}

                <section class="{{ counter of these loops }}" >
                    {% include "page.html" %}
                </section>

            {% endfor %}
        {% endfor %}

现在放入{{counter of these loops}}的地方可以获得循环计数器的数量。

如果第一个循环有4个元素,每个元素有5个子元素,那么现在在完成时我也必须有  counter = 20.

1 个答案:

答案 0 :(得分:1)

自定义过滤器中的

def multiply_rows(value, arg):
    return value*arg
模板中的

{{forloop.counter|multiply_rows forloop.parentloop.counter}} 

可能是您的解决方案。