user1768782224 user1768782224 - 2 years ago 266
Python Question

Django - Use forloop.counter0

I would like show element in my list on template html like that :

<table class="table table-striped col-md-12">
{% for row in list %}
<td>{{ row.0 }}</td>
<td>{{ row.1 }}</td>
<td>{{ ..... }}</td>
{% endfor %}

When the
evolve, I need to call new row manually.
So to have a generic call and dependent of the length on my list I choice the method
, but when I write my code, I have error or no elements on my screen.

{% for i in "xxx" %}
<td>{{ row.forloop.counter0 }}</td>
{% endfor %}

Answer Source

You shouldn't need to look up the index at all, just have another forloop inside that loops over the row

{% for i in row %}
   <td>{{ i }}</td>
{% endfor %}

If you need anything more granular, you might want to look into making an actual model to represent the data, and then iterate over a list of this model.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download