88dax
88dax

Reputation: 307

Django : user.is_authenticated, works without passing 'user'?

While I was following a tutorial, I came across this:
I have a template,'base.html' in which I didn't even pass the value for 'user' variable from a view. But still it authenticates the user. I can't understand how this works:
base.html :

{% if user.is_authenticated %}

<div id='nav'>
    <a href='/'/> mysite </a> |
    <a href='/user/{{user.username}}/'>{{ user.username }} </a>(<a href='/logout'>logout</a>)
</div>

{% endif %}
<h1>{% block head %}{% endblock %}</h1>
{% block content %}{% endblock %}

Upvotes: 0

Views: 87

Answers (2)

juankysmith
juankysmith

Reputation: 12448

You can access to the user object even if you don't pass it implicitly in the view response. By default it is an anonymous user, until an user log in and authenticates itself

Upvotes: 1

Hedde van der Heide
Hedde van der Heide

Reputation: 22449

You probably have the auth context processor and/or middleware set which adds the user variable to context

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
)

MIDDLEWARE_CLASSES = (
    "django.contrib.auth.middleware.AuthenticationMiddleware",
)

Upvotes: 0

Related Questions