Python Django Template: Iterate Through List


Technically it should iterate from 0 to rangeLength outputting the user name of the c[i][0].from_user...but from looking at example online, they seem to replace the brackets with dot notation. I have the following code:

<div id="right_pod">
{%for i in rangeLength%}
    <div class="user_pod" >
{% endfor %}

This currently outputs nothing :( If I replace "i" with 0...{{c.0.0.from_user}} will output something.. (the first user 10 times)

4/24/2009 12:37:01 AM

Accepted Answer

Do you need i to be an index? If not, see if the following code does what you're after:

<div id="right_pod">
{% for i in c %}
    <div class="user_pod">
        {{ i.0.from_user }}
{% endfor %}
4/24/2009 12:51:21 AM

Please read the entire documentation on the template language's for loops. First of all, that iteration (like in Python) is over objects, not indexes. Secondly, that within any for loop there is a forloop variable with two fields you'll be interested in:

Variable            Description
forloop.counter     The current iteration of the loop (1-indexed)
forloop.counter0    The current iteration of the loop (0-indexed)

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow