goh
goh

Reputation: 29571

django template strange behaviour

{% if None == False %}
    abc
{% endif %}

The above code, strangely my template displayed abc. Any explanation?

Upvotes: 3

Views: 195

Answers (5)

katy lavallee
katy lavallee

Reputation: 2771

Alternatively, you can do if not None.

Upvotes: 0

goh
goh

Reputation: 29571

Jason Culverhouse provided the answer in another similar question i asked.

False and None are treat as variables, instead of constants. If the variables ore not found in the context dictionary, there are resolved to None.

Upvotes: 3

solartic
solartic

Reputation: 4329

For now It seem like you will have to create your own template filter as was suggested in this post

Upvotes: 0

laher
laher

Reputation: 9110

This is just the way Python resolves 'truth' for comparison between different types.

See the docs. "None" is considered False.

http://docs.python.org/library/stdtypes.html#truth-value-testing

Edit: as below, the python console does not confirm this behaviour, so, I am also surprised. -1 to me!

Upvotes: 1

Leopd
Leopd

Reputation: 42769

Bizarre. In regular python,

if None == False:
    # this will not run
    print "abc"

File a bug on Django. :)

Upvotes: 3

Related Questions