Nosmoz RG
Nosmoz RG

Reputation: 15

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">
    <tbody>
        {% for row in list %}
        <tr>
            <td>{{ row.0 }}</td>
            <td>{{ row.1 }}</td>
            <td>{{ ..... }}</td>
        </tr>
        {% endfor %}
    </tbody>

When the len(list) 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 forloop.counter0, but when I write my code, I have error or no elements on my screen.

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

Upvotes: 0

Views: 732

Answers (1)

Sayse
Sayse

Reputation: 43320

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.

Upvotes: 1

Related Questions