goelv
goelv

Reputation: 2884

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

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"?

Upvotes: 149

Views: 161894

Answers (9)

Gerard
Gerard

Reputation: 9408

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 }}

Upvotes: 180

wsrt
wsrt

Reputation: 211

In cases where we need to validate for a field with null value, we may check so and process as under:

{% for field in form.visible_fields %}
    {% if field.name == 'some_field' %}
        {% if field.value is Null %}
            {{ 'No data found' }}
        {% else %}
            {{ field }}
        {% endif %}
    {% endif %}
{% endfor %}

Upvotes: 4

piertoni
piertoni

Reputation: 2021

Just a note about previous answers: Everything is correct if we want to display a string, but pay attention if you want to display numbers.

In particular when you have a 0 value bool(0) evaluates to False and so it will not display and probably is not what you want.

In this case better use

{% if profile.user.credit != None %}

Upvotes: 1

claypooj
claypooj

Reputation: 413

You could try this:

{% if not profile.user.first_name.value %}
  <p> -- </p>
{% else %}
  {{ profile.user.first_name }} {{ profile.user.last_name }}
{% endif %}

This way, you're essentially checking to see if the form field first_name has any value associated with it. See {{ field.value }} in Looping over the form's fields in Django Documentation.

I'm using Django 3.0.

Upvotes: 1

user6763622
user6763622

Reputation: 179

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

Upvotes: 17

Babacar Gningue
Babacar Gningue

Reputation: 1404

You can also use another built-in template default_if_none

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

Upvotes: 110

Louis
Louis

Reputation: 684

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

Upvotes: 20

mwjackson
mwjackson

Reputation: 5451

Look at the yesno helper

Eg:

{{ myValue|yesno:"itwasTrue,itWasFalse,itWasNone" }}

Upvotes: 6

Danica
Danica

Reputation: 28846

{% if profile.user.first_name %} works (assuming you also don't want to accept '').

if in Python in general treats None, False, '', [], {}, ... all as false.

Upvotes: 4

Related Questions