[Answered ]-Django template iterating over list

2👍

You can add an if statement checking if it is your 6th time through the loop.

{% for item in someList %}
{% if forloop.counter <= 6 %}
{{ item }}
{% endif %}
{% endfor %}

http://docs.djangoproject.com/en/1.3/ref/templates/builtins/#for in the docs.
Of course, if your list is very long then this is not optimal. I would also suggest processing the list in views.py and then passing it to the template. Logic should stay in the views if possible.

This gives you control over the number of loops done. To completely solve your problem you will need some addtional logic but see my note above regarding this.

0👍

Check this snippet: Template range filter

👤arie

Leave a comment