2011-03-11 14 views
8

Me gustaría saber cómo replicar esto en Django:Django forloop.counter operación

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

¿Cómo puedo comprobar el contador forloop si es "1"?

Respuesta

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

esto es equivalente a:

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