Reputation: 342
If you set a context variable (eg. 'woot') as None or just leave it undefined....
{% if woot %} Yeah! {% endif %}
Does what you'd expect (nothing). But if you do:
{% if woot == True %} Yeah! {% endif %}
It will print "Yeah!" even though woot is None / undefined. That seems very non-intuitive. Obviously, I can work around this... but I'd like to understand the root cause. Any ideas why it happens....?
Proof:
from django.template import Context, Template
x = Template("{% if woot %}Yeah!{% endif %}")
y = Template("{% if woot == True %}Yeah!{% endif %}")
x.render( Context( {} )) # => u''
y.render( Context( {} )) # => u'Yeah!'
x.render( Context( {'woot':None} )) # => u''
y.render( Context( {'woot':None} )) # => u'Yeah!'
This is on Django 1.4.3
Upvotes: 4
Views: 1264
Reputation: 309109
In Django 1.5 (release notes), the template engine interprets True
, False
and None
as the corresponding Python objects, so {% if woot == True %}
will evaluate to False
.
In earlier versions of Django, neither the woot
nor True
variables exist in the template context. The expression None == None
evaluates to True
, so Yeah! is displayed
Upvotes: 5