mssoft123 mssoft123 -4 years ago 102
Python Question

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 %}

Answer Source

You can just do

{% for x in var %}
    {{x}}
{% endfor %}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download