【问题标题】:django forloop.counter operationdjango forloop.counter 操作
【发布时间】:2011-07-13 16:06:26
【问题描述】:

我想知道如何在 django 中复制它:

for photo in gallery
    if counter is 1 
        then use this photo
     endif
endfor

如果forloop计数器为“1”,我如何检查它?

【问题讨论】:

    标签: django for-loop django-templates


    【解决方案1】:
    {% for photo in gallery %}
        {% if forloop.counter == 1 %}
           Do something with {{ photo }}.
        {% endif %}
    {% endfor %}
    

    这相当于:

    {% for photo in gallery %}
        {% if forloop.first %}
           Do something with {{ photo }}.
        {% endif %}
    {% endfor %}
    

    参考:Django docs

    【讨论】:

      猜你喜欢
      • 2019-02-26
      • 2012-07-04
      • 1970-01-01
      • 2010-11-16
      • 2011-01-30
      • 2014-07-23
      • 1970-01-01
      • 2022-08-07
      • 2019-04-16
      相关资源
      最近更新 更多