Django templates: If false?

后端 未结 10 1100
灰色年华
灰色年华 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 15:17

    You could write a custom template filter to do this in a half-dozen lines of code:

    from django.template import Library
    
    register = Library()
    
    @register.filter
    def is_false(arg): 
        return arg is False
    

    Then in your template:

    {% if myvar|is_false %}...{% endif %}
    

    Of course, you could make that template tag much more generic... but this suits your needs specifically ;-)

提交回复
热议问题