Authenticating data in templates
The currently logged-in user and their permissions are made available in the template context when you use RequestContext
.
Users
When rendering a template RequestContext
, the currently logged-in user, either a User
instance or an AnonymousUser
instance, is stored in the template variable
{{ user }}
:
{% if user.is_authenticated %} <p>Welcome, {{ user.username }}. Thanks for logging in.</p> {% else %} <p>Welcome, new user. Please log in.</p> {% endif %}
This template context variable is not available if a RequestContext
is not being used.
Permissions
The currently logged-in user's permissions are stored in the template variable
{{ perms }}
. This is an instance of Django.contrib.auth.context_processors.PermWrapper
, which ...
Get Mastering Django: Core now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.