How to combine for loops?
I'm making an HTML template in Django. For a list of animals and a range of numbers, I'd like to do something li开发者_JAVA百科ke this:
{% for animal in AnimalList and i in range%}
<p>{{i} {{animal.type}} </p>
{%endfor%}
but i get a template syntax error when i try something like that. The range could be from 6-10 or 3-7; the range's start and end points depend on some other functions that have happened elsewhere in the code.
You can use {forloop.counter} to get the times the loop has been entered and use the value from that other functions to increment this value.
Why not use two loops?
{% for animal in AnimalList %}
{% for i in range %}
<p>{{i} {{animal.type}} </p>
{%endfor%}
{%endfor%}
精彩评论