Python request.user 在 Django 中指的是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17312831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does request.user refer to in Django?
提问by himalayanZephyr
I have confusion regarding what does request.userrefers to in Django? Does it refer to usernamefield in the auth_usertable or does it refer to User model instance?
我对request.user在 Django 中指的是什么感到困惑?它是指auth_user表中的用户名字段还是指用户模型实例?
I had this doubt because I was not able to access email field in the template using {{request.user.username}}
or {{user.username}}
.
我有这个疑问,因为我无法使用{{request.user.username}}
或访问模板中的电子邮件字段{{user.username}}
。
So instead I did following in views file:
所以我在视图文件中做了以下操作:
userr = User.objects.get(username=request.user)
And passed userr
to the template and accessed email field as {{ userr.email }}
.
并传递userr
给模板和访问的电子邮件字段作为{{ userr.email }}
.
Although its working but I wanted to have some clarity about it.
虽然它有效,但我想对它有一些了解。
采纳答案by emigue
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 }}
.
如果您的模板正在接收AnonymousUser,{{request.user.email}}
则将找不到对 的引用。以前,您必须询问是否{{request.user.is_authenticated }}
.
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.
您必须检查它是否包含django.core.context_processors.auth
在TEMPLATE_CONTEXT_PROCESSORS
设置部分中的上下文处理器。如果您使用的是 Django 1.4 或最新版本,则上下文处理器是django.contrib.auth.context_processors.auth
. 这个上下文处理器负责在每个请求中包含用户对象。
回答by jammas615
request.user refers to the actual user model instance.
request.user 指的是实际的用户模型实例。
request.user.FIELDNAME will allow you to access all the fields of the user model
request.user.FIELDNAME 将允许您访问用户模型的所有字段
回答by falsetru
request.user
is User model object.
request.user
是用户模型对象。
You cannot access request object in template if you do not pass request
explicitly.
If you want access user object from template, you should pass it to template or use RequestContext.
如果不request
明确传递,则无法访问模板中的请求对象。如果要从模板访问用户对象,则应将其传递给模板或使用 RequestContext。
回答by suhailvs
It depends upon what you set .
这取决于您设置的内容。
So, it is better to use
所以,最好使用
user = User.objects.get(username=request.user.username)
Actually, you don't need to define such variables if you append 'django.core.context_processors.request'
into the TEMPLATE_CONTEXT_PROCESSORS
list in settings.py
实际上,如果您附加'django.core.context_processors.request'
到TEMPLATE_CONTEXT_PROCESSORS
列表中,则不需要定义此类变量settings.py
Then you can access the variable {{ request.user.username }} in templates if you are using render
in views.py
然后,你可以,如果你使用模板访问变量{{request.user.username}}render
在views.py