且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-06-26 07:49:21

您可以使用请求对象来查找已登录的用户

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

根据https://docs.djangoproject.com/en/2.0/releases/1.10/

在 Django 2.0 版本中,语法已更改为

In version Django 2.0 the syntax has changed to

request.user.is_authenticated