views:

2434

answers:

3

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" >
  {{c.i.0.from_user}}
 </div>
{% endfor %}

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

+3  A: 

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 }}
    </div>
{% endfor %}
Chris Doble
Ideally I need i to be an index so I can limit the amount of users to loop over. Should I be doing this in the Controller before passing c to the template?
TimLeung
Yes, you should.
Paolo Bergantino
+1 for doing this in the controller layer; the view shouldn't contain business logic like that.
Justin Voss
+1  A: 

You should use the slice template filter to achieve what you want:

Iterate over the object (c in this case) like so:

{% for c in objects|slice:":30" %}

This would make sure that you only iterate over the first 30 objects.

Also, you can use the forloop.counter object to keep track of which loop iteration you're on.

sotangochips
+1  A: 

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)
ironfroggy