Django templates: If false?

后端 未结 10 1144
灰色年华
灰色年华 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:53

    I've just come up with the following which is looking good in Django 1.8

    Try this instead of value is not False:

    if value|stringformat:'r' != 'False'
    

    Try this instead of value is True:

    if value|stringformat:'r' == 'True'
    

    unless you've been really messing with repr methods to make value look like a boolean I reckon this should give you a firm enough assurance that value is True or False.

提交回复
热议问题