user13505646
user13505646

Reputation:

for-loop in django

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

Answers (1)

xemeds
xemeds

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

Related Questions