How do I use 'is not None' in Django 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.
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 %}