Reputation:
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 %}
Upvotes: 2
Views: 137
Reputation: 316
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 %}
Upvotes: 3