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!
{% %} 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.
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.
{% 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.
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!
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 %}
.
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.
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 %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With