Memo/Python

[Django] 템플릿(html)에서 파이썬 enumerate 사용

l22hs 2023. 1. 30. 20:45

Django 템플릿에서 파이썬 enumerate 기능을 사용하려면 다음과 같이 코드를 for문 내부에 작성한다.

{% for n in network %}        
   {{ forloop.counter }}
{% endfor %}

위 코드는 1부터 시작하는 코드이다.

만약 파이썬의 enumerate 처럼 0부터 시작하고 싶다면 아래처럼 'counter'뒤에 0만 붙여주면 된다.

{% for n in network %}        
   {{ forloop.counter0 }}
{% endfor %}
 

how to run this code in django template

this is my code : {% for i,j in enumerate(a) %} {{i}} ,{{j}} {% endfor%} but , it show a error , i think it cant run the enumerate method , so how to run the enumerate in django template ,

stackoverflow.com