示例#1
0
    def post(self, request):
        # 处理
        # 1. 接收
        data = request.POST
        # 2. 处理
        # 验证是否合法
        form = RegisterModelForm(data)
        if form.is_valid():
            # 处理,保存到数据库
            data = form.cleaned_data
            # 密码需要加密
            password = data.get('password2')
            # 调用方法帮助我对密码进行加密
            password = set_password(password)
            # 保存到数据库
            SpUser.objects.create(phone=data.get('phone'), password=password)

            # 成功跳转到登陆页面
            return redirect("sp_user:login")
        else:
            # 传递错误信息到页面
            context = {
                "errors": form.errors,
            }
            # 3. 响应
            return render(request, "sp_user/reg.html", context)
示例#2
0
 def clean(self):
     # 获取所有清洗后的数据
     data = super(RegisterForm, self).clean()
     pwd1 = data.get('password')
     pwd2 = data.get('repassword')
     if pwd1 and pwd2 and pwd1 != pwd2:
         raise forms.ValidationError({'repassword': '******'})
     else:
         # 取密码
         if pwd1:
             # 如果密码不为空 进行加密
             data['password'] = set_password(pwd1)
     return data
示例#3
0
 def clean(self):
     # 所有清洗后的数据
     cleaned_data = super().clean()
     pwd1 = cleaned_data.get("password")
     pwd2 = cleaned_data.get("repassword")
     # 比较两次密码是否一致
     if pwd1 and pwd2 and pwd1 != pwd2:
         raise forms.ValidationError({"repassword": "******"})
     else:
         if pwd1:
             # 对密码加密
             cleaned_data['password'] = set_password(pwd1)
     # 返回所有清洗后的数据
     return cleaned_data
示例#4
0
 def post(self, request):
     # 接受数据
     data = request.POST
     form = RegisterModelForm(data)
     if form.is_valid():
         # 处理数据
         data = form.cleaned_data
         password = data.get('password2')
         password = set_password(password)
         Users.objects.create(phone=data.get('phone'), password=password)
         # 响应
         return redirect('sp_user:login')
     else:
         context = {'errors': form.errors}
         return render(request, 'sp_user/reg.html', context)
示例#5
0
 def clean(self):
     cleaned_data = super().clean()
     # 验证手机号码和密码是否正确
     phone = cleaned_data.get('phone')
     password = cleaned_data.get('password', "")
     # 通过手机查询数据,如果有就验证密码,没有则报错
     user = Users.objects.filter(phone=phone).first()
     if user is None:
         raise forms.ValidationError({"phone": "该号码没有注册"})
     else:
         # 存在手机号码
         password_in_db = user.password
         password = set_password(password)
         if password_in_db != password:
             raise forms.ValidationError({"password": "******"})
         else:
             # 保存用户的信息对象user,到cleaned-data
             cleaned_data['user'] = user
             return cleaned_data
示例#6
0
 def clean(self):
     cleaned_data = self.cleaned_data
     # 获取数据
     phone = cleaned_data.get('phone')
     password = cleaned_data.get('password')
     # 验证手机号码是否存在
     if all([phone, password]):
         # 根据手机号码获取用户
         try:
             user = Users.objects.get(phone=phone)
         except Users.DoesNotExist:
             raise forms.ValidationError({"phone": "该用户不存在!"})
         # 判断密码是否正确
         if user.password != set_password(password):
             raise forms.ValidationError({"password": "******"})
         # 将用户信息保存到cleaned_data中
         cleaned_data['user'] = user
         return cleaned_data
     else:
         return cleaned_data