프로그래밍/Python

[Django_Template] forloop 사용법

1q 2017. 9. 3. 12:49

Template 문법에서 indexing을 사용하고자 한다면, 아래와 같이 사용하면 된다.

{% for data in lists %}

  {{ data }} - {{ forloop.counter0 }}

{% endfor %}

lists 변수에 ['1', '2', '3']이 들어있다면, 1 - 0, 2 - 1, 3 - 2 값이 출력된다.

* 참고

forloop.counter - The current iteration of the loop (1-indexed)

forloop.counter0 - The current iteration of the loop (0-indexed)

forloop.revcounter - The number of iterations from the end of the loop (1-indexed)

forloop.revcounter0 - The number of iterations from the end of the loop (0-indexed)

forloop.first - True if this is the first time through the loop

forloop.last - True if this is the last time through the loop

forloop.parentloop - For nested loops, this is the loop surrounding the current one


참고 : https://docs.djangoproject.com/en/1.11/ref/templates/builtins/