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

Python django.contrib.auth login()返回无问题,python,django,django-rest-framework,web-development-server,Python,Django,Django Rest Framework,Web Development Server,我是django的新手,我一直在尝试为我的django项目实现登录系统 template_name = 'rest_framework/login.html' if request.method == 'POST': email = request.POST['username'] password = request.POST['password'] user = authenticate(request, username=e

我是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)
出于某种原因,我从django.contrib.auth导入的登录函数返回None,即使用户在身份验证后具有正确的用户对象,并且请求具有POST请求对象。这会导致用户未被添加到会话中,因此当我将其重定向到核心:主页时,我在request.user中获得了匿名用户。我们将不胜感激


PS:我已经编写了一个用于身份验证的自定义后端。

实际上
login()
方法没有返回类型。之前必须检查
authenticate()
方法的结果。如果身份验证失败,
authenticate()
方法返回None。在这种情况下,意味着没有提供用户名和密码的用户。所以,如果
authenticate()
的结果为None,您可以发出警告,并且不要调用
login()
方法。

login(…)
应该返回
Nothing
。只有
authenticate
检查凭据,如果Credentail失败,则返回
None
。噢,谢谢。我不知道这件事。但是,问题仍然存在,authenticate()为我提供了正确的用户对象。但是,当我将它重定向到另一个URL时,我得到的是匿名用户