def register(request): """注册页面""" res = '' if request.method == 'POST': username = request.POST.get('username') psw = request.POST.get('password') mail = request.POST.get('mail') # 查询数据库中是否由此用户名 user_list = User.objects.filter(user_name=username) if user_list: # 如果已经注册给提示 res = '{}用户已经被注册!'.format(username) return render(request, 'register.html', {'rename': res}) else: # 如果没有注册过,进行注册 # 第一种写法 (建议) user_new = User() user_new.user_name = username # 加密方法 user_new.psw = make_password(psw) user_new.mail = mail user_new.save() # 第二种写法 # user_new = User(user_name=username, # psw=psw, # mail=mail) # user_new.save() return render(request, 'login.html', {'rename': res}) return render(request, 'register.html')
def register(request): '''注册页面''' res = "" if request.method == "POST": username = request.POST.get('username') psw = request.POST.get('password') mail = request.POST.get('mail') # 先查询数据库是否有此用户名 user_lst = User.objects.filter(user_name=username) if user_lst: # 如果已经注册过,就给个提示 res = "%s用户已被注册" % username return render(request, 'register.html', {'rename': res}) else: # 如果没被注册,插入数据库 # 第一种写法 -- 推荐 user = User() user.user_name = username user.psw = psw user.mail = mail user.save() # 第二种写法 # user = User(user_name=username, # psw = psw, # mail = mail, # ) # user.save() return render(request, 'login.html', {'rename': res}) return render(request, 'register.html')
def reset_pwd(request): """修改密码""" res = '' if request.method == 'GET': return render(request, 'reset_pwd.html', {'msg': res}) if request.method == 'POST': # 获取页面中值 username = request.POST.get('username') pwd = request.POST.get('password') new_pwd = request.POST.get('new') if pwd == new_pwd: res = '新密码和旧密码不能重复!' return render(request, 'reset_pwd.html', {'msg': res}) else: # 查询数据库是否有此用户名 user_list = User.objects.filter(user_name=username) if not user_list: # 如果没有此用户 res = '{}用户未注册'.format(username) return render(request, 'reset_pwd.html', {'msg': res}) else: # 如果注册过,判断密码对不对 ret = User.objects.filter(user_name=username).first() # 校验密码 is_pwd_true = check_password(pwd, new_pwd) if is_pwd_true: user = User() user.psw = make_password(new_pwd) user.save() res = '密码修改成功!' else: res = '密码错误!' return render(request, 'reset_pwd.html', {'msg': res})
def reset_psw(request): '''修改密码''' res = "" if request.method == "GET": return render(request, 'reset_psw.html', {'msg': res}) if request.method == "POST": username = request.POST.get('username') psw = request.POST.get('password') new_psw = request.POST.get('new') if psw == new_psw: res = "新密码和旧密码不能重复" return render(request, 'reset_psw.html', {'msg': res}) else: # 先查询数据库是否有此用户名 user_lst = User.objects.filter(user_name=username) if not user_lst: # 如果没这个用户 res = "用户未注册:%s" % username return render(request, 'reset_psw.html', {'msg': res}) else: # 如果注册过,判断密码对不对 ret = User.objects.filter(user_name=username).first() # 校验密码 is_psw_true = check_password(psw, ret.psw) print(psw) print(ret.psw) print(is_psw_true) if is_psw_true: user = User() user.psw = make_password(new_psw) user.save() res = "密码修改成功!" else: res = "密码错误!" return render(request, 'reset_psw.html', {'msg': res})