django登录认证错误
def register(request): if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') print(username,password) if User.objects.filter(username=username): user_status['status'] = False user_status['msg'] = '请更换用户名!' return HttpResponse(json.dumps(user_status)) elif username == '' or password== '': user_status['status'] = False user_status['msg'] = '用户名和密码不能为空!' else: User.objects.create_user(username=username,password=password) user_status['status'] = True user_status['msg'] = '注册成功!' return HttpResponse(json.dumps(user_status))
使用auth.authenticate(username= username,passowrd=passowrd),这个用户认证时候,明明数据库中有记录,但是返回就None
在配置注册用户的时候使用了User.objects.create('username=username,password=password)
1.并没有添加到在auth_user(django自带的表)中,而是放进了通过modely映射生成的表,从而导致auth.authenticate()根本查不到响应的数据
2.插入数据用户名密码时应该用User.objects.create_user(username=username,password=password),这个方法会把密码生成哈希值,插进数据库,而不能用User.objects.create(。。。。),这样插进去的数据密码是明文
正确用法:User.objects.create_user(username=username,password=password)