【问题标题】:Single print statement during loop iteration in DjangoDjango循环迭代期间的单个打印语句
【发布时间】:2015-08-04 01:22:38
【问题描述】:

我正在处理一个 Django 模板,我需要在循环迭代期间打印一次我的语句。我试过{% ifchanged %},但它不能在两个循环中工作。

使用{% ifchanged %} 在单个循环下工作,但我在两个循环内尝试。

例如:

{% for i in j %}
    {% for k in j %}
        {% ifchanged %}
            //something here//
        {% endifchanged %}
    {% endfor %}
{% endfor %}

但是,在这种情况下,它不起作用。

【问题讨论】:

    标签: python django django-templates


    【解决方案1】:

    还有forloop.first

    请参阅the Django Built-in template tags and filters documentation 了解与 forloop 相关的变量。

    {% if forloop.first %}
        // something here //
    {% endif %}
    

    还有一个forloop.last,如果它需要出现在最后,还有一个forloop.counter

    【讨论】:

      猜你喜欢
      • 2011-11-24
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 2022-01-08
      • 1970-01-01
      相关资源
      最近更新 更多