def post(self, request): # 接收参数 data = request.POST form = PasswordForm(data) # 验证数据的合法性 if form.is_valid(): # 获取清洗后的数据 cleaned = form.cleaned_data # 取出清洗后的密码 # 将密码进行加密 password = set_password(cleaned.get('password')) # 通过id查询数据 user_id = request.session.get('ID') # print(user_id) # 修改到数据库 # 验证原密码是否存在,不能用get,用filter if Users.objects.filter(id=user_id, password=password).exists(): password2 = set_password(cleaned.get('password2')) # 更新密码 Users.objects.filter(id=user_id).update(password=password2) # 跳转到登录页 return redirect('users:登录') else: # 错误 return render(request, 'users/password.html', context={ 'errors': form.errors, })
def post(self, request): # 完成用户信息的注册 # 接收参数 data = request.POST # 验证参数合法性 表单验证 form = RegisterForm(data) if form.is_valid(): # 获取清洗后的数据 cleaned = form.cleaned_data # 将密码进行加密 # 取出清洗后的手机号 mobile = cleaned.get('mobile') # 取出清洗后的密码 password = set_password(cleaned.get('password')) # print(password) # 修改到数据库 data = {'mobile': mobile, 'password': password} Users.objects.create(**data) # 跳转到登录页 return redirect('users:登录') else: # 错误 return render(request, self.template_name, context={ 'errors': form.errors, })
def post(self, request): # 接收参数 data = request.POST # 验证数据的合法性 form = ForgetForm(data) if form.is_valid(): # 获取清洗后的数据 cleaned = form.cleaned_data # 将密码进行加密 # 通过id查询数据 user_id = request.session.get('ID') # 取出清洗后的手机号 mobile = cleaned.get('mobile') # 取出清洗后的密码 password2 = set_password(cleaned.get('password2')) # 修改到数据库 # 验证原密码是否存在,不能用get,用filter if Users.objects.filter(mobile=mobile, id=user_id).exists(): # 更新密码 Users.objects.filter(id=user_id).update(password=password2) # 跳转到登录页 return redirect('users:登录') else: # 错误 return render(request, 'users/forgetpassword.html', context={ 'errors': form.errors, })
def post(self, request): data = request.POST form = RegisterForm(data) if form.is_valid(): cleaned = form.cleaned_data name = cleaned.get('name') password = set_password(cleaned.get('password')) data = {'name': name, 'password': password} User.objects.create(**data) return redirect('user:登录') else: context = {'errors': form.errors} return render(request, 'user/reg.html', context=context)
def post(self, request): # 接收参数 data = request.POST # 验证数据的合法性 form = LoginForm(data) if form.is_valid(): # id # form.verify_password(request) ################# 写在helper.py里面封装了 # session验证登录 # user = form.cleaned_data.get('user') # request.session['ID'] = user.id # request.session['mobile'] = user.mobile # request.session.set_expiry(0) # /////////////////// user = form.cleaned_data.get('user') login(request, user) ################ # 获取清洗后的数据 cleaned = form.cleaned_data # 将密码进行加密 # 取出清洗后的手机号 mobile = cleaned.get('mobile') # 取出清洗后的密码 password = set_password(cleaned.get('password')) # 从数据库查询 try: Users.objects.get(mobile=mobile, password=password) referer = request.session.get('referer') if referer: # 跳转回去 # 删除session del request.session['referer'] return redirect(referer) else: # 跳转到登录页 return redirect('users:个人中心') except: return render(request, "users/login.html") else: # 不合法 context = { 'errors': form.errors, } return render(request, "users/login.html", context=context)
def clean(self): # 验证用户名 name = self.cleaned_data.get('name') password = self.cleaned_data.get('password') # 验证手机号 try: user = User.objects.get(name=name) except User.DoesNotExist: raise forms.ValidationError({'name': '用户名'}) # 验证密码 if user.password != set_password(password): raise forms.ValidationError({'password': '******'}) # 用于session验证登录 self.cleaned_data['user'] = user # 返回,返回整个清洗后的数据 return self.cleaned_data
def post(self, request): data = request.POST form = LoginForm(data) if form.is_valid(): user = form.cleaned_data.get('user') login(request, user) cleaned = form.cleaned_data name = cleaned.get('name') password = set_password(cleaned.get('password')) try: User.objects.get(name=name, password=password) referer = request.session.get('referer') if referer: del request.session['referer'] return redirect(referer) else: return redirect('blog:博客首页' "" "") except: return render(request, 'user/login.html') else: # 不合法 context = { 'errors': form.errors, } return render(request, "user/login.html", context=context)