How does template in django get the user object?

心不动则不痛 提交于 2019-12-23 17:42:36

问题


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?


回答1:


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',
)


来源:https://stackoverflow.com/questions/31896103/how-does-template-in-django-get-the-user-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!