niloofar
niloofar

Reputation: 2334

django if tag & ifequal tag not working correctly

views.py:

def get(request):
    p = Publisher.objects.filter(name='tux')
    return render(request, 'main.html', {'items': p[0]})

main.html:

<html>    
    <body>
    {{ items }}
    <hr>
    {% if 'tux' in items %}
       <h1>this is tux</h1>
    {% else %}
       <h1>sorry!</h1>
    {% endif %}
    </body>    
</html>

What is printed on the webpage:

tux


sorry!

And what about if I want to use {% ifequal %} tag? What syntax should be used? I tried this:

{% ifequal {{items}} 'tux' %}

and it turned parse error, and I also tried this:

{% ifequal items 'tux' %}

but the result was again:

tux


sorry!

Upvotes: 2

Views: 586

Answers (1)

chem1st
chem1st

Reputation: 1634

You cannot use it like this: {% if 'tux' in items %}, since items is an object. Use {% if 'tux' in items.name %} instead. The reason you get 'tux' displayed in the case of {{items}} is that your model returns name field as a unicode representation.

Upvotes: 5

Related Questions