mssoft123
mssoft123

Reputation: 43

Loop through a Python list in Django template with for loop

I need to simplify this Django template,

{{ var.1 }}
{{ var.2 }}
{{ var.3 }}
{{ var.4 }}
{{ var.5 }}

var is Python list passed as context to the template

how do you convert the above template using a for tag construct. I tried this but does not work.

 {% for i in var|length %}
     {{ var.i }}
 {% endfor %}     

Upvotes: 1

Views: 2898

Answers (1)

karthikr
karthikr

Reputation: 99620

You can just do

{% for x in var %}
    {{x}}
{% endfor %}

Upvotes: 4

Related Questions