如果你将上下文变量(例如 'woot')设置为None或只是未定义......{% if woot %} Yeah! {% endif %}将不会有任何输出。但如果你这样做:{% if woot == True %} Yeah! {% endif %}即使woot是None/未定义,它也会打印出“Yeah!”。这似乎非常不直观。显然,我可以解决这个问题...但我想了解根本原因。你有什么想法吗?证明如下:
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!'
这是针对Django 1.4.3版本的