What is the equivalent of "none" in django templates? What is the equivalent of "none" in django templates? python python

What is the equivalent of "none" in django templates?


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.pyclass 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 :)


You can also use another built-in template default_if_none

{{ profile.user.first_name|default_if_none:"--" }}


isoperator : 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 %}