Angela Kravcevich
Angela Kravcevich

Reputation: 19

Django if statement

Is it possible to use this code

{% if item.status == "0" %}
                <td>Pending</td>
            {% else %}
                <td>Approved</td>
            {% endif %}

if the item.status is an integer? It seems to never go into the if statement and prints else all the time Should i declare the variable first? eg something = item.status?

If yes then what is the correct syntax?

Upvotes: 1

Views: 7934

Answers (3)

JamesO
JamesO

Reputation: 25936

this should work.

{% ifequal item.status 0 %}
     <td>Pending</td>
{% else %}
     <td>Approved</td>
{% endifequal %}

Edit

just to clarify (as the other answers have mentioned) the issue is "0", comparing int == string, ifequal is just my preferred way of using the template tag.

Upvotes: 2

Evan Porter
Evan Porter

Reputation: 2977

Remove the quotes around the 0 and it looks like it should work. See here

Upvotes: 10

Uku Loskit
Uku Loskit

Reputation: 42040

You are comparing it to a string "0", not am integer 0. That's the problem. The syntax is fine, just remove the quotes.

Upvotes: 4

Related Questions