Hi all,
I’m passing a list of objects to a Template, I need to check for a specific condition in each object,
otherwise a different message should be shown.
It any programming/scripting language it would be very easy, but I’m struggling:
{% for obj in object_watchlist %}
{% if obj.status == obj2.status %}
status is equal
{% endif %}
{% endfor %}
How do I know when to print “status is NOT equal” ?
object_watchlist
is your list of objects right ?
and are you also passing obj2
from your views to templates ? Like from where this obj2
comes.
yes, both are coming from views.py
This is just a skeleton of my code of course, but it shows what I need to do, I need to basically either
“status equal” or “not equal” while looping through a list of objects
this condition seems correct, here you can use {% else %} before {% endif %} to print if the status is not equal.
No, I need to establish if obj.status is NOT equal to obj2.status for each obj
if I do:
{% for obj in object_watchlist %}
{% if obj.status == obj2.status %}
status is equal
{% else %}
status NEVER equal
{% endif %}
{% endfor %}
it wouldn’t work
The line “status NEVER equal” should be printed only if there’s NEVER a match in the for loop.
Usually I would do it with a variable like:
set status_var = false
{% for obj in object_watchlist %}
{% if obj.status == obj2.status %}
status is equal
status_var=true
{% else %}
{% endif %}
{% endfor %}
if status_var== false
status NEVER equal
I hope it makes sense.
Not getting what you want but if you want to check not equal than you can do something like {% if obj.status != obj2.status %}
or {% if not obj.status == obj2.status %}
Typically in this type of situation, where the template needs to render something based upon a “condition of the whole”, I recommend making that determination in the view and setting some value accordingly, where that value can be tested in the template.