1

How can I do for-loop like this in Django?

list = ['AAA', 'BBB', 'CCC']

==========================================

{% for x in len(list), for y in list %}

<p>{{x}}: {{y}}</p>

{% endfor %}
bahdotsh
  • 394
  • 1
  • 13
  • 1
    Does this answer your question? [Accessing the index in 'for' loops?](https://stackoverflow.com/questions/522563/accessing-the-index-in-for-loops) – Wups Oct 10 '20 at 07:31

1 Answers1

2

The best option is the enumerate function.

return render_template("index.html", list=enumerate(list))

Then in the template:

{% for index, value in list %}

    <p>{{index}}: {{value}}</p>

{% endfor %}
xemeds
  • 158
  • 12