Django Template Ternary Operator

后端 未结 6 816
慢半拍i
慢半拍i 2020-12-29 19:32

I was wondering if there was a ternary operator (condition ? true-value : false-value) that could be used in a Django template. I see there is a python one (true-value if c

6条回答
  •  生来不讨喜
    2020-12-29 19:46

    Why would you need a ternary operator within a template? {% if %} and {% else %} are all you need.

    Or you could try the firstof tag:

    {% firstof var1 var2 var3 %}
    

    which outputs the first one of var1, var2 or var3 which evaluates to a True value.

提交回复
热议问题