Django templates: If false?

后端 未结 10 1135
灰色年华
灰色年华 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条回答
  •  旧时难觅i
    2020-12-23 15:15

    Just ran into this again (certain I had before and came up with a less-than-satisfying solution).

    For a tri-state boolean semantic (for example, using models.NullBooleanField), this works well:

    {% if test.passed|lower == 'false' %} ... {% endif %}
    

    Or if you prefer getting excited over the whole thing...

    {% if test.passed|upper == 'FALSE' %} ... {% endif %}
    

    Either way, this handles the special condition where you don't care about the None (evaluating to False in the if block) or True case.

提交回复
热议问题