Django Template Ternary Operator

Anon picture Anon · Jun 24, 2010 · Viewed 28.2k times · Source

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?

Answer

jeroenp picture jeroenp · May 22, 2011

You can use the yesno filter:

{{ value|yesno:"yeah,no,maybe" }}

You can learn more here