2011-03-11 23 views
8

Tôi muốn biết làm thế nào để tái tạo này trong django:django forloop.counter hoạt động

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

làm thế nào để tôi kiểm tra quầy forloop nếu nó là "1"?

Trả lời

27
{% for photo in gallery %} 
    {% if forloop.counter == 1 %} 
     Do something with {{ photo }}. 
    {% endif %} 
{% endfor %} 

này tương đương với:

{% for photo in gallery %} 
    {% if forloop.first %} 
     Do something with {{ photo }}. 
    {% endif %} 
{% endfor %} 
Các vấn đề liên quan