django中的模板如何获取用户对象? [英] How does template in django get the user object?

查看:189
本文介绍了django中的模板如何获取用户对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

模板如何获取用户对象?

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?

推荐答案

使用 django.contrib.auth.context_processors.auth 上下文处理器,您可以访问模板中的 auth.User 实例。

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


如果 TEMPLATE_CONTEXT_PROCESSORS 包含此处理器,则每个
RequestContext 将包含以下变量:

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

user –一个 auth.User 代表当前登录用户
的实例(如果客户端未登录,则为AnonymousUser实例)。

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

只需在 TEMPLATE_CONTEXT_PROCESSORS 设置中定义 django.contrib.auth.context_processors.auth ,然后使用 {{user}}

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
....
)

模板上下文处理器:


它是用于在
RequestContext中填充上下文
的可调用元组。这些可调用对象将请求对象作为其
参数,并返回要合并到
上下文中的项的字典。

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.

默认情况下,以下上下文处理器由Django 1.6设置。

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")

模板中存在哪些变量?

您可以知道 TEMPLATE_CONTEXT_PROCESSORS 设置将所有变量显示在所有模板中。其中定义的每个上下文处理器都将一些变量包含到上下文中。例如, django.contrib.auth.context_processors.auth 包含一个 user 变量,该变量包含 user 对象, django.core.context_processors.media 上下文处理器在模板中包含 MEDIA_URL 变量。

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.

要检查使用不同上下文处理器在模板中可访问哪些所有变量,请参阅此 Django文档链接。

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

访问<$ c上下文中的$ c>请求对象

Accessing the request object in the context

您可以添加 django.core.context_processors.request settings.py 中的code> TEMPLATE_CONTEXT_PROCESSORS ,然后访问您的请求对象上下文。

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

您还可以以 {{request.user}} 的身份访问当前用户。您必须显式添加此设置,因为默认情况下不存在此设置。

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.

.request 上下文处理器添加到 TEMPLATE_CONTEXT_PROCESSORS

Add .request context processor to TEMPLATE_CONTEXT_PROCESSORS in your settings.

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

编辑:(感谢 @Ozgur

(Thanks @Ozgur)

另外,添加 <您的 user 属性的 MIDDLEWARE_CLASSES 设置中的code> AUTHENTICATION_MIDDLEWARE 可以在请求对象中进行设置。它已从MIDDLEWARE_CLASSES 设置中删除。 rel = nofollow noreferrer> Django 1.7。

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

添加 user 属性,代表每个登录的 HttpRequest 对象的当前登录用户。

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

这篇关于django中的模板如何获取用户对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆