[Django]-Caught TypeError while rendering: 'int' object is not iterable in django template

5👍

I think you are trying to print index of every row in the table. For that you have to iterate over a list of indices. Create this list in your django view

list = []
for i in range(0,list_length):
    list.append(i)

Then pass this list in your django template and iterate over it

{% for i in list %}
    <tr>
        <td>{{ i }}</td>
    </tr>
{% endfor %}

This should work for you.

0👍

A list itself may be iterable, but a single integer on its own is not a list.

If you want to iterate from zero up to an integer, you can use something like Python’s range(n) to create an iterator for those values. That’s for naked Python, Django appears to require a slightly more complex method, as per Numeric for loop in Django templates.

Otherwise, you possibly need to iterate over the list itself rather than its length (depending on what you’re trying to do and whether you need a list ‘index’ within the loop).

Leave a comment