Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python authenticate()函数不起作用django.contrib.auth_Python_Django_Django Authentication - Fatal编程技术网

Python authenticate()函数不起作用django.contrib.auth

Python authenticate()函数不起作用django.contrib.auth,python,django,django-authentication,Python,Django,Django Authentication,我有一个login_page函数,在这个函数中,authenticate()函数仅当用户对象是超级用户时才返回用户对象。对于普通用户,它返回None。这并不像文件所说的那样 def login_page(request): if request.user.is_authenticated(): # if user is already logged in return HttpResponseRedirect('/') # SHOULD BE DASHBOARD

我有一个login_page函数,在这个函数中,authenticate()函数仅当用户对象是超级用户时才返回用户对象。对于普通用户,它返回None。这并不像文件所说的那样

def login_page(request):
    if request.user.is_authenticated(): # if user is already logged in
        return HttpResponseRedirect('/') # SHOULD BE DASHBOARD
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            seo_specialist = authenticate(username=username, password=password) #returns None
            if seo_specialist is not None:
                login(request, seo_specialist)
                return HttpResponseRedirect('/') # SHOULD BE DASHBOARD
            else:
                return render(request, 'login.html', {'form': form})
        else:
            return render(request, 'login.html', {'form': form})
    else: 
        form = LoginForm()
        context = {'form': form}
        return render(request, 'login.html', context)
我的代码有什么问题吗?

试试这个:

def login_page(request):
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']
        seo_specialist = authenticate(username=username, password=password)
        if seo_specialist is not None:
            return HttpResponse("Signed in")
        else:
            return HttpResponse("Not signed in")
    else:
        # takes you to sign in form. 
基本上,用request.POST替换有效且已清理的\u数据,然后进行身份验证。还要确保你有

从django.contrib.auth导入验证


在您的视图顶部

这来自django文档。您似乎没有在…身份验证(请求、用户…)中传递请求

此示例显示如何使用authenticate()和login()


如果authenticate返回None,则用户名和密码是错误的。如果只是对超级用户进行身份验证,则您的数据库中除了超级用户之外没有其他用户。我已经检查并重新检查了这一点……您能给我们一个无法进行身份验证的用户的对象转储吗?@mariodev我如何生成用户对象的转储?对不起,我第一次发布时,代码缩进被弄乱了,但我想你仍然可以看到我在做什么。将你正在谈论的Django版本粘贴到你的答案中,发布它。这个问题已经7年了,从那以后发生了很多变化。审查结束。
    from django.contrib.auth import authenticate, login

    def my_view(request):
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            # Redirect to a success page.
            ...
        else:
            # Return an 'invalid login' error message.
            ...