简体   繁体   English

如何在 Django 中获取登录用户的用户名?

[英]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 ,前者是首选。

Django docs say: Django 文档说:

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.

相关问题 如何在Django views.py中获取登录用户的用户名 - How can I get the username of the logged-in user in Django views.py 如何使用Django将一个页面显示给已登录的用户,将另一个页面显示给未登录的用户? - How do I show one page to logged-in user and another to a not-logged in user with Django? 如何将登录用户重定向到 Django 中的特定页面 - How to redirect logged-in user to a specific page in Django 如何获取写入文本的登录用户的用户名并将其永久显示在 Django 中的文本旁边? - How do i get the username of a logged user writing a text and display it beside the text permanently in Django? 如何将登录用户的用户名添加到 django 中的模型字段 - How do I add username of logged in user to model field in django Django 获取登录用户的用户名并将其传递给模型 - Django get username of logged in user and pass it to models 如何从 django 用户模型中获取用户名 - How can I get a username from django User Model 如果登录Django,如何获取用户ID? - How I get User ID if logged in Django? 每次提交表单时如何将登录用户的用户名放入数据库中 - How to put the username of the logged-in user in the databse each time a form is submitted 如何使用 django 添加登录用户的用户名 - How to add the logged in user's username with django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM