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?
6 Answers
-
1Brilliant. Thank you for the answer. I knew this existed, but couldn't remember. Jan 27, 2013 at 14:04
-
11Very nice and simple, but when I want to use some variables it seems this filter is not applicable. For instance I would like to use
{{ expr_or_value | yesno : "the string", other_variable_value }}
– ivkremerApr 20, 2015 at 15:14 -
3This should be the selected answer! This is exactly what's being asked.– PepedouMay 26, 2017 at 2:30
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.
-
1I suppose you're right, I'll just use if/else. Just addicted to ternary I guess.– AnonJun 24, 2010 at 13:14
-
17
-
59I tend to disagree with Daniel. I recently converted a django template to a simple javascript template:
{% if data_complete %}{{ data }}{% else %}{{ something_else }}{% endif %}
became<%= data_complete ? data : something_else %>
... the second is so much more compact and elegant. It would be nice if there was an equivalent in django. Apr 27, 2013 at 15:51 -
14"why would you" ... well, to do something in 30 sec instead of 10mn, for instance. Django templating isn't so developer-friendly, it's not always a good thing to have to do stuff in the view instead of the template. Aug 5, 2018 at 0:53
Just because they haven't been mentioned here yet: the built in template tags default
, and default_if_none
can be useful in simple circumstances:
default
If value evaluates to False, uses the given default. Otherwise, uses the value.
For example:
{{ value|default:"nothing" }}
If value is "" (the empty string), the output will be nothing.
default_if_none
If (and only if) value is None, uses the given default. Otherwise, uses the >value.
Note that if an empty string is given, the default value will not be used. Use >the default filter if you want to fallback for empty strings.
For example:
{{ value|default_if_none:"nothing" }}
If value is None, the output will be the string "nothing".
https://docs.djangoproject.com/en/1.9/ref/templates/builtins/#default
-
-
2
You don't. The Django {% if %}
templatetag has only just started supporting ==
, and
, etc. {% if cond %}{% else %}{% endif %}
is as compact as it gets for now.
I've just implemented the ternary operator for Django as a tag, see https://github.com/alexei/django-template-extensions You can use it as:
{% ?: exp1 exp2 exp3 %}
{% ?: exp1 exp2 %}
Or:
{% iif exp1 exp2 exp3 %}
{% iif exp1 exp2 %}
I figured out that it makes more sense than the yesno
filter, even though it's really not that Pythonic.
-
Awesome, thanks! Solves the issue that I can't pass context variables to
|yesno
– ptimApr 14, 2014 at 3:36
I wonder if the python and/or trick would work?
condition and true_value or false_value
behaves a like the ternary operator - outputs true_value if condition evaluates to True, and false_value if not.
-
1I learnt recently that
true_value if condition else false_value
is also valid Python syntax, and it's much more readable -- will check if it works in templates...– hwjpMar 9, 2012 at 11:30 -
2