What is the equivalent of "none" in django templates?
Solution 1
None, False and True
all are available within template tags and filters. None, False
, the empty string ('', "", """"""
) and empty lists/tuples all evaluate to False
when evaluated by if
, so you can easily do
{% if profile.user.first_name == None %}
{% if not profile.user.first_name %}
A hint: @fabiocerqueira is right, leave logic to models, limit templates to be the only presentation layer and calculate stuff like that in you model. An example:
# someapp/models.py
class UserProfile(models.Model):
user = models.OneToOneField('auth.User')
# other fields
def get_full_name(self):
if not self.user.first_name:
return
return ' '.join([self.user.first_name, self.user.last_name])
# template
{{ user.get_profile.get_full_name }}
Hope this helps :)
Solution 2
You can also use another built-in template default_if_none
{{ profile.user.first_name|default_if_none:"--" }}
Solution 3
You can also use the built-in template filter default
:
If value evaluates to False (e.g. None, an empty string, 0, False); the default "--" is displayed.
{{ profile.user.first_name|default:"--" }}
Documentation: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#default
Solution 4
is
operator : New in Django 1.10
{% if somevar is None %}
This appears if somevar is None, or if somevar is not found in the context.
{% endif %}
Solution 5
Look at the yesno helper
Eg:
{{ myValue|yesno:"itwasTrue,itWasFalse,itWasNone" }}
goelv
Updated on July 28, 2021Comments
-
goelv almost 3 years
I want to see if a field/variable is none within a Django template. What is the correct syntax for that?
This is what I currently have:
{% if profile.user.first_name is null %} <p> -- </p> {% elif %} {{ profile.user.first_name }} {{ profile.user.last_name }} {% endif%}
In the example above, what would I use to replace "null"?
-
tobych over 10 yearsI just tried to pass None as a parameter to the {% cache %} tag and learned that it's not available. Busy working around this.
-
Rockallite over 10 years
{% if profile.user.first_name is None %}
causes syntax error in Django template. -
jbg over 10 yearsYour hint mixes presentation and logic by putting HTML in your model, doing exactly the opposite of what you are trying to teach. How about returning None if there is no name (logical behaviour for a data model) and then using the
default_if_none
filter in the template? -
Gerard over 10 years@JasperBryant-Greene you are right! just updated. Good catch man! Thx!
-
dbn over 9 yearsdefault_if_none would probably be a better option.
-
Andreas Bergström over 6 yearsAny idea how to use this with other filters such as date? For example, is it possible to display "N/A" if a date is none but otherwise format it? Like: {{ post.pub_date|default_if_none:"N/A"|date:"Y-m-d" }}?
-
defance over 5 years@AndreasBergström In your case you apply
date
first. If any error occurs it will return None. Then you applydefault_if_none
. -
User about 4 yearsWhat version of Django was this brought in?
-
gregory almost 2 yearsIf the output is an empty string, this works while default_if_none does NOT. Choose carefully.