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

查看:23
本文介绍了如何使用 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 返回上例中最里面的 for 循环计数器

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天全站免登陆