如何使用Django模板中嵌套for循环访问最外层forloop.counter? [英] How to access outermost forloop.counter with nested for loops in Django templates?

查看:878
本文介绍了如何使用Django模板中嵌套for循环访问最外层forloop.counter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以访问Django中以下模板中最外层for循环的forloop.counter:

Is it possible to access the forloop.counter for the outermost for loop in the following template in Django:

{% for outerItem in outerItems %}
    {% for item in items%}
        <div>{{ forloop.counter }}.&nbsp;{{ item }}</div>
    {% endfor %}
{% endfor %}

forloop.counter在上面的例子中返回最里面的循环计数器

forloop.counter returns the innermost for loop's counter in the above example

推荐答案

你可以使用 forloop.parentloop 获得外部 forloop ,所以在你的情况下 {{forloop.parentloop.counter}}

You can use forloop.parentloop to get to the outer forloop, so in your case {{forloop.parentloop.counter}}.

这篇关于如何使用Django模板中嵌套for循环访问最外层forloop.counter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆