views:

41

answers:

1

The following code snippet in a Django template (v 1.1) doesn't work.

{{ item.vendors.all.0 }} ==> returns "Test"

but the following code snippet, doesn't hide the paragraph!

{% ifnotequal item.vendors.all.0 "Test" %}
<p class="view_vendor">Vendor(s): {{item.vendors.all.0}} </p><br />
{% endifnotequal %}

Any tips on what's wrong?

Thanks.

+6  A: 

item.vendors.all.0 doesn't return "Test": It returns a vendor object, which gives "Test" when converted to a string. If you just compare the object with "Test", it will never be equal.

Try converting the object to a string before comparing:

{% ifnotequal item.vendors.all.0|stringformat:"s" "Test" %}
interjay
Or `ifnotequal item.vendors.all.0.name "Test"`, where `name` is the relevant model field.
Daniel Roseman
I realized it's returning "vendor" object, i.e. the __unicode__ method is called when I print but not when I compare them. But when I print "name", the model field its empty...But the solution works fine. Thanks.
lud0h