将登录表单中输入的密码与数据库中的密码匹配不起作用

时间:2011-10-27 10:52:14

标签: django django-authentication

我使用django documentaion中定义的方法:http://www.djangobook.com/en/beta/chapter12/

def login(request):
    m = members.get_object(username__exact=request.POST['username'])
    if m.password == request.POST['password']:
        request.session['member_id'] = m.id
        return HttpResponse("You're logged in.")
    else:
        return HttpResponse("Your username and password didn't match.")

但听起来它与密码不正确!我输入了有效的用户名和密码,但是找不到用户,当我删除这一行时,它可以工作:

if m.password == request.POST['password']:

我认为因为密码是经过哈希处理的,所以它与用户在登录表单中输入的普通密码不匹配。

所以,我现在该怎么办?

2 个答案:

答案 0 :(得分:4)

您应该使用user.check_password。原因是密码存储了哈希,你无法直接比较它。

P.S。抓住认证后端的工作方式。

答案 1 :(得分:1)

这不应该是如何进行身份验证的示例。你会发现真实的例子有点further down the page