开发者

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%}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜