【问题标题】:Entering wrong username and password still django authenticate it as correct输入错误的用户名和密码仍然 django 将其验证为正确
【发布时间】:2022-01-31 09:25:26
【问题描述】:

当登录详细信息正确填写时,我正在尝试在 Django 中创建登录页面,它重定向到某个页面我面临的问题是,当我输入错误的用户名或密码时,Django 将它们验证为正确并将其重定向到另一个页面我的登录代码如下

def loginpage(request):
    if request.method=='POST':
        use=request.POST.get('username')
        pas=request.POST.get('password')
        
        
        user=authenticate(request,username=use,password=pas)
        if user is not NONE:
            login(request,user)
            return redirect('data')
            
        else:
            messages.info(request,'Username or Password is incorrect')
            return render(request,'login.html')

即使在输入数据库中甚至不存在的用户名之后,Django 也验证为正确并且没有显示我期望的错误

【问题讨论】:

  • 出于好奇,您为什么将request 作为第一个参数传递给authenticate() 函数?该参数默认设置为无。

标签: html python-3.x django authentication django-views


【解决方案1】:

您使用大写的 NONE 而不是 None。您一定是从pickle 或其他库中意外导入了NONE,这就是它进行验证而不是引发错误的原因。检查文件顶部的导入并更改为if user is not None。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    相关资源
    最近更新 更多