简体   繁体   English

如何从django.contrib.auth(Django内置登录系统)获取用户名?

[英]How to get the username from django.contrib.auth (Django built-in login system)?

I am very new to Python and Django, and am looking at a code that uses the Django built-in login system (django.contrib.auth.login). 我是Python和Django的新手,并且正在查看使用Django内置登录系统(django.contrib.auth.login)的代码。

After the user has logged in, I would like to redirect the user to an appropriate URL based on the users privilege. 用户登录后,我想根据用户权限将用户重定向到适当的URL。 For example, some usernames have admin privileges, while some are just regular users. 例如,某些用户名具有管理员权限,而某些用户只是普通用户。 However, after the "login" button is clicked, the user is always redirected to the same URL. 但是,单击“登录”按钮后,始终会将用户重定向到相同的URL。

In order to redirect the user differently, I would have to check the username, and check what privileges that user has. 为了以不同的方式重定向用户,我将必须检查用户名,并检查该用户具有哪些特权。 However, I can not find any way to fetch the username from django.contrib.auth.login. 但是,我找不到从django.contrib.auth.login获取用户名的任何方法。 It is as if the username just disappears after it has been authenticated. 好像用户名在通过身份验证后就消失了。

Does anyone know how to solve this problem? 有谁知道如何解决这个问题?

Thank you! 谢谢!

用户名可在以下位置找到:

request.user.username

Well if you're able to log him you should have something like that (partial code here): 好吧,如果您能够登录他,您应该具有类似的内容(此处为部分代码):

user = authenticate(username=username, password=password)
login(request, user)

if user.is_admin and user.is_staff:
    return HttpRedirectResponse('/some/path')
elif user.idontknow:
    return HttpRedirectResponse('/some/other/path')
...
else:
    return HttpRedirectResponse('/default/redirect/url')

The login view supports the "next" parameter. 登录视图支持“ next”参数。 So in your form template you can add it like this: 因此,您可以在表单模板中添加以下内容:

<form method="POST">
    {% csrf_token %}
    <input type="hidden" name="next" value="/go/here/if/successful" />
    {{ form }}
</form>

To do this in the view, while respecting the next parameter and provide different defaults based on the user: 要在视图中执行此操作,同时要考虑下一个参数并根据用户提供不同的默认值:

from django.contrib.auth.views import LoginView

class MyLoginView(LoginView):
    def get_redirect_url(self):
        next = super(MyLoginView, self).get_redirect_url()
        if not next:
            user = self.request.user  # see form_valid and auth.login()
            if user.is_staff:
                return '/staff/goes/here'

        return next

In login template some like: 在登录模板中,例如:

<form action="/login/" method="post">
    <input type="text" name="username" placeholder="username"/>
    <input type="password" name="password" placeholder="password"/>
</form>

And in backend like django docs says: 并且在django docs这样的后端中说:

from django.contrib.auth import authenticate
user = authenticate(username='john', password='secret')
if user is not None:
    # A backend authenticated the credentials
else:
    # No backend authenticated the credentials

You can use following code in views and bind url to index function 您可以在视图中使用以下代码并将url绑定到索引功能

from django.shortcuts import render

def index(request):
    if request.user.is_admin and request.user.is_active:
        return render(request, 'admin-page.html')
    return render(request, 'user-page.html')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM