Python 如何在 Django 中获取登录用户的用户名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16906515/
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
How can I get the username of the logged-in user in Django?
提问by Cris Towi
How can I get information about the logged-in user in a Django application?
如何在 Django 应用程序中获取有关登录用户的信息?
For example:
例如:
I need to know the username of the logged-in user to say who posted a Review:
我需要知道登录用户的用户名才能说出谁发布了评论:
<form id='formulario' method='POST' action=''>
<h2>Publica tu tuit, {{ usuario.username.title }} </h2>
{% csrf_token %}
{{formulario.as_p}}
<p><input type='submit' value='Confirmar' /></p>
</form>
In usuario.username.titleI get the username, but in the template, I need to get that information from the view.
在usuario.username.title我获得用户名时,但在模板中,我需要从视图中获取该信息。
Thanks. =)
谢谢。=)
回答by karthikr
You can use the request object to find the logged in user
您可以使用请求对象来查找登录用户
def my_view(request):
username = None
if request.user.is_authenticated():
username = request.user.username
According to https://docs.djangoproject.com/en/2.0/releases/1.10/
根据https://docs.djangoproject.com/en/2.0/releases/1.10/
In version Django 2.0 the syntax has changed to
在 Django 2.0 版本中,语法已更改为
request.user.is_authenticated
回答by Luan Fonseca
if you are using the old way of writting views, in the way of Function-Based-Views...
如果您使用旧的编写视图的方式,以基于函数的视图的方式...
in your view, you are creating a new variable called usuarioto save the request.user probably...
在您看来,您正在创建一个名为usuario保存请求的新变量。用户可能...
but if you returning to the Templatea context_instance, passing the value of the Context of the request, you will get the logged user, just by accessing the request.
但是如果您返回Templatea context_instance,传递请求的上下文的值,您将获得登录的用户,只需访问request.
// In your views file
from django.shortcuts import render_to_response
from django.template import RequestContext
def your_view(request):
data = {
'formulario': Formulario()
# ...
}
return render_to_response('your_template.html',
data, context_instance=RequestContext(request))
// In your template
<form id='formulario' method='POST' action=''>
<h2>Publica tu tuit, {{ request.user.username.title }} </h2>
{% csrf_token %}
{{ formulario.as_p }}
<p><input type='submit' value='Confirmar' /></p>
</form>
回答by ancho
'request.user' has the logged in user.
'request.user.username' will return username of logged in user.
' request.user' 具有登录用户。
' request.user.username' 将返回登录用户的用户名。
回答by user
request.user.get_username()or request.user.username, former is preferred.
request.user.get_username()或者request.user.username,前者是首选。
Since the User model can be swapped out, you should use this method instead of referencing the username attribute directly.
由于 User 模型可以换出,您应该使用此方法而不是直接引用 username 属性。
P.S. For templates, use {{ user.get_username }}
PS 对于模板,使用 {{ user.get_username }}
回答by Dan Walters
request.user.get_username()will return a string of the users email.
request.user.get_username()将返回用户电子邮件的字符串。
request.user.usernamewill return a method.
request.user.username将返回一个方法。
回答by sakabio
For classed based views use self.request.user.id
对于基于分类的视图使用 self.request.user.id

