Django模板三元运算符 [英] Django Template Ternary Operator
问题描述
我想知道是否有可以在Django模板中使用的三元运算符(condition?true-value:false-value)。我看到有一个python一个(如果条件else false-value是真值),但我不确定如何在Django模板中使用它来显示其中一个值给出的html。任何想法?
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 condition else false-value) but I'm unsure how to use that inside a Django template to display the html given by one of the values. Any ideas?
推荐答案
为什么在模板中需要一个三元运算符?所有您需要的 {%if%}
和 {%else%}
Why would you need a ternary operator within a template? {% if %}
and {% else %}
are all you need.
或者您可以尝试 firstof
标签:
{% firstof var1 var2 var3 %}
输出var1,var2或var3中的第一个其评估为True值。
which outputs the first one of var1, var2 or var3 which evaluates to a True value.
这篇关于Django模板三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!