JohnJ
JohnJ

Reputation: 7056

django skip entry when looping over queryset (in template)

I have a ListView model, which generates me the required queryset. Then, I my template, I do something like:

{% for i in queryset %}

{{i.var1}}

{% endfor %}

.. and this works perfectly. But, what I want to do is:

{% for i in queryset %}

{% if i.var1 == "mystring" %}

 <skip this entry and do i++>

{% else %}

{{i.var2}}

{% endfor %}

.. but how do I <skip this entry and do i++>?

Upvotes: 0

Views: 176

Answers (2)

Sayse
Sayse

Reputation: 43320

If you need to do this sort of editing then it isn't working perfectly, you should filter these entries out of your queryset inside of your view

.exclude(var1="mystring")

In the comments you state that the queryset comes from an iterable, so at the very least you'd be able to do list comprehension in the view

[i for i in queryset if i.var1 != "mystring"]

If this is something you absolutely have to do in a template, then the only way is with the complete surrounding if statement that you already have in your question

Upvotes: 0

mayur
mayur

Reputation: 33

this may help you

{% if i.var1 != "mystring" %}

this simply skip mystring and loop counter also increase

Upvotes: 2

Related Questions