Python django中的check_password()始终返回false
我正在制作一个自定义重置密码模块。我正在用django auth用户模型检查旧密码。但是check_密码总是返回false。如果我的逻辑有错误,请帮忙 views.pyPython django中的check_password()始终返回false,python,django,django-authentication,Python,Django,Django Authentication,我正在制作一个自定义重置密码模块。我正在用django auth用户模型检查旧密码。但是check_密码总是返回false。如果我的逻辑有错误,请帮忙 views.py def user_pass_change(request): pass_old = request.POST.get('old_pass') hash_pass_new = make_password(request.POST.get('new_pass')) username = request.POS
def user_pass_change(request):
pass_old = request.POST.get('old_pass')
hash_pass_new = make_password(request.POST.get('new_pass'))
username = request.POST.get('username')
user = User.objects.get(username=username)
if user:
check = check_password(user.password,pass_old)
if check:
User.objects.filter(username=username).update(password=hash_pass_new)
messages.success(request, 'Password changed !')
return redirect('/login')
else:
messages.warning(request, 'Wrong old password')
return redirect('/login')
else:
messages.warning(request, 'Invalid Username !')
return redirect('/login')
我已尝试在setting.py中包含这些哈希值
PASSWORD_HASHERS = [
'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
'django.contrib.auth.hashers.BCryptPasswordHasher',
'django.contrib.auth.hashers.PBKDF2PasswordHasher',
'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
'django.contrib.auth.hashers.Argon2PasswordHasher',
]
提前感谢
用户。密码
是密码的散列,而不是实际密码<代码>检查密码需要原始字符串
要检查当前密码是否与传递旧密码相同,可以执行以下操作:
check = user.check_password(pass_old) # user is the User object
您如何定义/导入
检查密码()
?据我所知,该函数是User
模型的一个成员,并且只接受一个参数,如:User.check\u password(pass\u old)
,但这里您将其作为一个独立函数使用,它接受两个参数。这解决了问题吗<代码>用户。检查密码(旧密码)