[英]How can I get the username of the logged-in user in Django?
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.title
I get the username, but in the template, I need to get that information from the view.在usuario.username.title
我得到用户名,但在模板中,我需要从视图中获取该信息。
Thanks.谢谢。 =) =)
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
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 属性。
PS For templates, use {{ user.get_username }}
PS 对于模板,使用{{ user.get_username }}
' request.user ' has the logged in user. ' request.user ' 具有登录用户。
' request.user.username ' will return username of logged in user. ' request.user.username ' 将返回登录用户的用户名。
对于基于分类的视图使用 self.request.user.id
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 usuario
to save the request.user probably...在您看来,您正在创建一个名为usuario
的新变量来保存 request.user 可能...
but if you returning to the Template
a context_instance
, passing the value of the Context of the request, you will get the logged user, just by accessing the request
.但是如果你返回到Template
一个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>
request.user.get_username()
will return a string of the users email. request.user.get_username()
将返回用户电子邮件的字符串。
request.user.username
will return a method. request.user.username
将返回一个方法。
You can use this to get the logged-in user's username :-您可以使用它来获取登录用户的用户名:-
Just write this in template.只需在模板中写这个。
{{ request.user.username }}
in this way you can check is user is logged in or not If yes for example go to profile if not back to login page通过这种方式,您可以检查用户是否已登录如果是,例如如果没有返回登录页面,请转到个人资料
def profile(request):
try:
if request.user.username in User:
return render(request, "profile.html", {'username': request.user.username})
except:
return redirect("/accounts/login")
For template, you can use对于模板,您可以使用
{% firstof request.user.get_full_name request.user.username %}
firstof will return the first one if not null else the second one如果不为空,则 firstof 将返回第一个,否则返回第二个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.