Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use 'is not None' in Django Templates?

I want to customize the greeting on my homepage so it's whether:

When logged in and entered first name:

John! Where do you want to go?

else:

Where do you want to go?

This is my template:

 {% if user.is_authenticated and not firstname_of_logged_user == None%}
 <h1 class="text-center" id="extraglow">{{firstname_of_logged_user}}! where do 
 you want to go?</h1></label>
{%else%}
<h1 class="text-center" id="extraglow">where do you want to go?</h1></label>
                                {%endif%}

However, it doesn't seem to pick up the "not None" part because if the user is logged in but didn't enter first name it shows like this:

! where do you want to go?

and if I say:

firstname_of_logged_user is not None

it says an error:

Unused 'is' at end of if expression.

Seems like an easy thing but it's not working. What's wrong? Cheers!

like image 1000
Seif Avatar asked Mar 18 '16 14:03

Seif


People also ask

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.

Does not exist template?

Django TemplateDoesNotExist error means simply that the framework can't find the template file. To use the template-loading API, you'll need to tell the framework where you store your templates. The place to do this is in your settings file ( settings.py ) by TEMPLATE_DIRS setting.

Why is {% include %} used?

{% include %} Processes a partial template. Any variables in the parent template will be available in the partial template. Variables set from the partial template using the set or assign tags will be available in the parent template.

Which option does Django templates accept?

DjangoTemplates engines accept the following OPTIONS : 'autoescape' : a boolean that controls whether HTML autoescaping is enabled. It defaults to True . Only set it to False if you're rendering non-HTML templates!


3 Answers

The if template tag has supported the is operator since Django 1.10 (release notes).

You can now do {% if x is None %} and {% if x is not None %}.

like image 108
Alasdair Avatar answered Oct 02 '22 06:10

Alasdair


You can use the != operator instead of not. However, things can be simplified even further to:

{% if user.is_authenticated and firstname_of_logged_user %}

The above will check whether or not firstname_of_logged_user is truthy in your template.

like image 41
rnevius Avatar answered Oct 02 '22 06:10

rnevius


The is operator is not supported. Just use {% if var != None %}. This is not exactly the same but it will be sufficient for most cases.

See supported operators here.

If you actually want to use the is operator, you would have to implement it as a custom template tag.

For instance:

@register.filter(name='is')
def do_is(lhs, rhs):
    return lhs is rhs

Thus you will be able to use it as:

{% if var1|is:var2 or not var3|is:var4 %}
    ...
{% else %}
    ...
{% endif %}
like image 33
Antoine Pinsard Avatar answered Oct 02 '22 04:10

Antoine Pinsard