K Bob
K Bob

Reputation: 39

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 like 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.

Upvotes: 0

Views: 185

Answers (2)

Dominik
Dominik

Reputation: 377

Why not use two loops?

{% for animal in AnimalList %}
    {% for i in range %}
        <p>{{i} {{animal.type}} </p>
    {%endfor%}
{%endfor%}

Upvotes: 0

David Moreno Garc&#237;a
David Moreno Garc&#237;a

Reputation: 4523

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.

Upvotes: 1

Related Questions