Django templates: If false?

mpen picture mpen · Nov 19, 2010 · Viewed 107.3k times · Source

How do I check if a variable is False using Django template syntax?

{% if myvar == False %}

Doesn't seem to work.

Note that I very specifically want to check if it has the Python value False. This variable could be an empty array too, which is not what I want to check for.

Answer

Bialecki picture Bialecki · Jul 26, 2011

For posterity, I have a few NullBooleanFields and here's what I do:

To check if it's True:

{% if variable %}True{% endif %}

To check if it's False (note this works because there's only 3 values -- True/False/None):

{% if variable != None %}False{% endif %}

To check if it's None:

{% if variable == None %}None{% endif %}

I'm not sure why, but I can't do variable == False, but I can do variable == None.