JT.
JT.

Reputation: 351

using boolan in django templates

for item in query_results:
    num +=1
    print num

this will give you 1, 2, 3, 4 etc

I then tried doing this in django templates doing the following

{% for item in query_results %}
<tr>
<td>{{ item.user }}</td>
<td>{{ num|add:"1" }}</td>
</tr>
{% endfor %}

But this only returns 1, 1, 1, 1, 1 etc. This says to me that the 1 isn't being saved to num each cycle. IS this then not a capability of django templates, or am i just doing it wrong.

Upvotes: 1

Views: 62

Answers (2)

juliomalegria
juliomalegria

Reputation: 24951

The built-in add filter just adds the argument to the value, but doesn't modify it. That's why you're getting always 1 as result.

More about it: https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#add

Upvotes: 0

Stephen Diehl
Stephen Diehl

Reputation: 8439

Use forloop.counter instead.

{% for item in query_results %}
<tr>
<td>{{ item.user }}</td>
<td>{{ forloop.counter }}</td>
</tr>
{% endfor %}

Upvotes: 8

Related Questions