开发者

django template strange behaviour

开发者 https://www.devze.com 2023-02-25 11:00 出处:网络
{% if None == False %} abc 开发者_运维技巧{% endif %} The above code, strangely my template displayed abc. Any explanation?Bizarre.In regular python,
{% if None == False %}
    abc
开发者_运维技巧{% endif %}

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


Bizarre. In regular python,

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

File a bug on Django. :)


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.


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!


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


Alternatively, you can do if not None.

0

精彩评论

暂无评论...
验证码 换一张
取 消