Django templates: If false?

后端 未结 10 1133
灰色年华
灰色年华 2020-12-23 14:43

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

{% if myvar == False %}

Doesn\'t seem to work.

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-23 14:52

    This is far easier to check in Python (i.e. your view code) than in the template, because the Python code is simply:

    myvar is False
    

    Illustrating:

    >>> False is False
    True
    >>> None is False
    False
    >>> [] is False
    False
    

    The problem at the template level is that the template if doesn't parse is (though it does parse in). Also, if you don't mind it, you could try to patch support for is into the template engine; base it on the code for ==.

提交回复
热议问题