티스토리 뷰

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/

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함