Nips
Nips

Reputation: 13860

How to count in template (not forloop)?

I have forloop in my template and i use forloop.counter but in this case:

{% for item in items %}
    {% if item.name %}
        {{ forloop.counter }} - {{ item.name }}
    {% endif %}
{% endfor %}

forloop failed. It shown:

1 - bla, bla
5 - woo, woo
...

How to count only visible items?

Upvotes: 1

Views: 511

Answers (1)

Elf Sternberg
Elf Sternberg

Reputation: 16361

When confronted with this problem, I would pass a "list of visible items" to the template. If you need both lists on the page for some reason, then pass both lists to the template. One is just a filtered queryset of another.

Upvotes: 2

Related Questions