request.user 在 Django 中指的是什么? [英] What does request.user refer to in Django?

查看:21
本文介绍了request.user 在 Django 中指的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 request.user 在 Django 中指的是什么感到困惑?它是指 auth_user 表中的 username 字段还是指用户模型实例?

I have confusion regarding what does request.user refers to in Django? Does it refer to username field in the auth_user table or does it refer to User model instance?

我有这个疑问,因为我无法使用 {{request.user.username}}{{user.username}} 访问模板中的电子邮件字段>.

I had this doubt because I was not able to access email field in the template using {{request.user.username}} or {{user.username}}.

所以我在视图文件中做了以下操作:

So instead I did following in views file:

userr = User.objects.get(username=request.user)

并将 userr 传递给模板并以 {{ userr.email }} 的形式访问电子邮件字段.

And passed userr to the template and accessed email field as {{ userr.email }}.

虽然它有效,但我想对它有一些了解.

Although its working but I wanted to have some clarity about it.

推荐答案

如果您的模板正在接收 AnonymousUser,将找不到对 {{request.user.email}} 的引用.以前,您必须询问是否 {{request.user.is_authenticated }}.

If your template is receiving AnonymousUser, the reference to {{request.user.email}} will not be found. Previously, you must ask if {{request.user.is_authenticated }}.

您必须检查它是否包含在设置的 TEMPLATE_CONTEXT_PROCESSORS 部分中的 django.core.context_processors.auth 上下文处理器.如果您使用的是 Django 1.4 或最新版本,则上下文处理器是 django.contrib.auth.context_processors.auth.这个上下文处理器负责在每个请求中包含用户对象.

You must check if it is included django.core.context_processors.auth context processor in TEMPLATE_CONTEXT_PROCESSORS section of settings. If you are using Django 1.4 or latest, then context processor is django.contrib.auth.context_processors.auth. This context processor is responsible to include user object in every request.

这篇关于request.user 在 Django 中指的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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