我有一个登录和身份验证功能,它从表单中获取输入,验证数据库中的数据,如果正确,则返回 right.html,如果不正确,则返回错误的.html。密码存储在galaxy_user数据库中,uname的输出为:
u'sachitad'
passw 的输出是:
u'f8566297ee28e8a3096497070b37b91d24c24243'
def login(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
u = ''.join(username)
p = hashlib.sha1(password).hexdigest()
a = GalaxyUser.objects.values_list('username', 'password')
uname = a[0][0]
passw = a[0][1]
user = authenticate(uname=u, passw=p)
if user is not None:
return render_to_response('right.html', context_instance=RequestContext(request))
else:
return render_to_response('wrong.html', context_instance=RequestContext(request))
else:
return render_to_response('login.html', context_instance=RequestContext(request))
编辑: Galaxy用户表:
id | create_time | update_time | email | password | external | deleted | purged | username | form_values_id | disk_usage |
+----+---------------------+---------------------+-------------------------------+------------------------------------------+----------+---------+--------+----------+----------------+------------+
| 1 | 2013-01-11 15:00:30 | 2013-01-11 15:00:30 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2c7c1d7c083f2d5dfd3dbde9cd1dddf" rel="noreferrer noopener nofollow">[email protected]</a> | f8566297ee28e8a3096497070b37b91d24c24243 | 0 | 0 | 0 | sachitad | NULL | NULL |
| 2 | 2013-01-11 15:01:01 | 2013-01-11 15:01:01 | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5c292f392e6e1c26292e39302f333a28723f3331" rel="noreferrer noopener nofollow">[email protected]</a> | f8566297ee28e8a3096497070b37b91d24c24243 | 0 | 0 | 0 | saugat | NULL | NULL |
+----+---------------------+---------------------+-------------------------------+------------------------------------------+----------+---------+--------+-
我想使用电子邮件和密码进行身份验证。
最佳答案
Django 使用给定算法('md5'、'sha1' 或 'crypt')将密码存储为给定明文密码和盐的十六进制摘要
authenticate() 采用您在注册用户时提供的密码,而不是其哈希版本。您需要提供纯文本密码来验证用户身份。
关于django - Django 中的身份验证不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14272859/