当我注册一个新用户时,他的密码在数据库中用sha1方法加密。 当我想要搜索如果一个用户是或不是已经注册在数据库中为我的登录页面,我必须检查是否电子邮件地址存在,然后如果是发送的密码是好的。 这是我的代码:*views.py:“
#Login
@api_view(['POST', ])
def log_in(request):
if request.method == 'POST':
data = {}
email = request.POST.get('email')
password = request.POST.get('password')
password = hashlib.sha1(password.encode('utf-8'))
account = memberArea.objects.filter(email = email, password = password)
if account.exists():
for account in account:
data['succes'] = "Successfully connected"
data['id'] = account.id
data['email'] = account.email
else :
data['error'] = "email and password doesn't match !"
return Response(data)
在这里,我尝试对用户发送的密码进行加密,然后在数据库中查找这个加密后的密码。 经过测试,它不起作用。 谢谢你帮了我。
这里有3件东西要拆开。
用户
模型有set_password
,用于检查密码是否正确,有check_password
您没有显示如何注册用户,但是如果您的用户模型使用AbstractBaseUser
,那么您可能需要这样做。
此外,还有一个authenticate
函数,它根据您在设置中配置后端的方式来完成这一切。
您可以在中从django.contrib.auth import authenticate
找到它