简体   繁体   English

django.contrib.auth login() 返回无问题

[英]django.contrib.auth login() returning None issue

I am new to django and I have been trying to implement login system for my django project.我是 django 的新手,我一直在尝试为我的 django 项目实施登录系统。


    template_name = 'rest_framework/login.html'
    if request.method == 'POST':
        email = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username=email, password=password)
        print (login(request,user))
        login(request,user)
        return redirect('core:home')
    return render(request, template_name)

For some reason, the login function which I have imported from django.contrib.auth is returning None even when user has the correct user object after authenticating and request has POST request object. For some reason, the login function which I have imported from django.contrib.auth is returning None even when user has the correct user object after authenticating and request has POST request object. This is resulting in the user not getting added to the session and hence when I redirect it to core:home , I am getting AnonymousUser in request.user.这导致用户没有被添加到 session 中,因此当我将其重定向到core:home时,我在 request.user 中获得了 AnonymousUser。 Help would be appreciated.帮助将不胜感激。

PS: I have written a custom backend for authentication. PS:我已经为身份验证编写了一个自定义后端。

Actually login() method hasn't a return type.实际上login()方法没有返回类型。 You must check result of authenticate() method before.您必须先检查authenticate()方法的结果。 If authentication is failed, authenticate() method returns None.如果身份验证失败,则authenticate()方法返回 None。 In this situation means that, there is no user with provided username and password.在这种情况下,意味着没有提供用户名和密码的用户。 So if result of authenticate() is None, you can give warning and don't call login() method.所以如果authenticate()的结果是 None,你可以给出警告并且不要调用login()方法。

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

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