Reputation: 27559
If you look at the piece of code below, you will understand exactly what I want. Enough even if I only get the number of cars that comply with this condition. Or it would be great if we could create a new loop from cars that comply with this condition.
{% for car in cars %}
{% if car.color == 'white' %}
create new for loop from white cars
or
give me the numbers of white cars
{% endif %}
{% endfor %}
Upvotes: 0
Views: 74
Reputation: 635
You can use regroup
template tag to group the list of cars by the color and then select the list of cars with white color to iterate over.
{% regroup cars by color as cars_grouped_by_color %}
{% for cars in cars_grouped_by_color %}
{% if cars.grouper == 'white' %}
{% for car in cars.list %}
...
{% endfor %}
{% endif %}
{% endfor %}
Refer regroup documentation for more details.
Upvotes: 1
Reputation: 674
It is not possible to do it in a template. Modify your ORM query statement.
white_cars = Car.objects.filter(color='white')
And use it in your template with single loop.
{% for car in white_cars %}
// Your code
{% endfor %}
Upvotes: 1