Django comes with some template context processors - which are simply functions that insert variables into every template that is rendered on your site.
As @Jack states, one of these is called django.core.context_processors.auth
. This inserts a variable called user
into every template, and is enabled by default.
Therefore, to find out if a user is logged in or not, you should use this code in your templates:
{% if user.is_authenticated %}
You're logged in.
{% else %}
You're not logged in.
{% endif %}
The problem with using the code that Jack gave, is that the user
variable always exists - so that will always evaluate to True
(so long as you are using the django.core.context_processors.request
context processor, which is not enabled by default). Therefore, to find out if the user is actually logged in, you must use the is_authenticated()
method.