Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does template in django get the user object?

How does template get user object?

In other words what process exactly during rendering passes user object to template?

And what else is accessible in template?

like image 996
Brian Avatar asked Mar 16 '23 03:03

Brian


1 Answers

Using the django.contrib.auth.context_processors.auth context processor, you can access the auth.User instance in your template.

If TEMPLATE_CONTEXT_PROCESSORS contains this processor, every RequestContext will contain these variables:

user – An auth.User instance representing the currently logged-in user (or an AnonymousUser instance, if the client isn’t logged in).

Just define django.contrib.auth.context_processors.auth in your TEMPLATE_CONTEXT_PROCESSORS settings and then use {{user}} in your template.

TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth", # define this in your settings
....
)

Template Context Processors:

Its a tuple of callables that are used to populate the context in RequestContext. These callables take a request object as their argument and return a dictionary of items to be merged into the context.

By default the following context processors are set by Django 1.6.

("django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages")

What variables are present in the template?

You can know which all variables are present in all the template by the TEMPLATE_CONTEXT_PROCESSORS settings. Each context processor defined in it includes some variables into the context. For example, django.contrib.auth.context_processors.auth includes a user variable containing the user object, django.core.context_processors.media context processor includes MEDIA_URL variable in the template.

To check what all variables are accessible in a template using different context processors, refer this Django documentation link.

Accessing the request object in the context

You can add django.core.context_processors.request to TEMPLATE_CONTEXT_PROCESSORS in settings.py and access the request object in your context.

You can also access the current user as {{ request.user }}. You will have to explicitly add this setting as it is not present by default.

Add .request context processor to TEMPLATE_CONTEXT_PROCESSORS in your settings.

TEMPLATE_CONTEXT_PROCESSORS = (
    ....
    `django.core.context_processors.request`,
    )

EDIT: (Thanks @Ozgur)

Also, add the AUTHENTICATION_MIDDLEWARE in your MIDDLEWARE_CLASSES settings for user attribute to be set in the request object. It was removed from the default MIDDLEWARE_CLASSES settings in Django 1.7.

class AuthenticationMiddleware
Adds the user attribute, representing the currently-logged-in user, to every incoming HttpRequest object.

MIDDLEWARE_CLASSES = (
    ...
    # explicitly add the 'AuthenticationMiddleware' class
    'django.contrib.auth.middleware.AuthenticationMiddleware',
)
like image 174
Rahul Gupta Avatar answered Mar 23 '23 07:03

Rahul Gupta