async def post(self, *args, **kwargs): r_data = {} params = self.request.body.decode("utf-8") params = json.loads(params) form = RegisterForm.from_json(params) if form.validate(): mobile = form.mobile.data code = form.code.data password = form.password.data # check if the register code is valid redis_key = "{}_{}".format(mobile, code) # redis 内存存储,没太多必要使用异步查询。当然也可以使用 aioredis if not self.redis_conn.get(redis_key): self.set_status(400) r_data["code"] = "验证码错误或者失效" else: try: # if the mobile number has been used user = await self.application.objects.get(User, mobile=mobile) self.set_status(400) r_data["mobile"] = "用户已经存在" except User.DoesNotExist as e: user = await self.application.objects.create( User, mobile=mobile, password=password) r_data["id"] = user.id else: self.set_status(400) for field in form.errors: r_data[field] = form.errors[field][0] self.finish(r_data)
def get(self, request): """ 注册页面get访问 :param request:HttpRequest对象 :return: 返回HttpResponse对象或者render() """ register_form = RegisterForm() context = {'form': register_form} return render(request, 'front/register.html', context=context)
def create_user(request): if request.method == 'POST': # telephone=request.POST.get('telephone') # name=request.POST.get('name') # sex=request.POST.get('sex') # id_card=request.POST.get('id_card') # email = request.POST.get('email1') # password=request.POST.get('password') # print(telephone,name,sex,id_card,email,password) form = RegisterForm(request.POST) if form.is_valid(): telephone = form.cleaned_data.get('telephone') name = form.cleaned_data.get('name') password = form.cleaned_data.get('password') sex = form.cleaned_data.get('sex') id_card = form.cleaned_data.get('id_card') email = request.POST.get('email1') if email: user = User.objects.create_user(name=name, telephone=telephone, sex=sex, id_card=id_card, email=email, password=password) else: user = User.objects.create_user(name=name, telephone=telephone, sex=sex, id_card=id_card, password=password) user.save() errors = ['添加成功!'] else: errors = [] for value in form.get_errors().values(): if value[0] == 'This field is required.': errors.append('必填项不能为空!') else: errors.append(value[0]) errors = set(errors) return render(request, 'detail/suadmin/cre_user.html', {'errors': list(errors)}) else: return render(request, 'detail/suadmin/cre_user.html')
def post(self, request): """ 注册页面的post访问 接收注册表单的请求,验证后会写入数据库 注册成功后重定向到front:index,在session中传递info='注册成功' :param request: :return: """ form = RegisterForm(request.POST) if form.is_valid(): student = UserModel.objects.create(**form.cleaned_data) student.is_superuser = False student.save() request.session['info'] = '注册成功' my_print('用户:{:}注册成功'.format(student.name)) return redirect(reverse('front:login')) else: error = form.get_first_error() request.session['error_info'] = error win32api.MessageBox(0, error, u'提示', win32con.MB_OK) return redirect(reverse('front:register'))
async def post(self): ret_data = {} registerForm = RegisterForm(self.request.arguments) if registerForm.validate(): username = registerForm.username.data try: exist_user = await self.application.objects.get( User, username=username) ret_data["username"] = "******" except User.DoesNotExist as e: user = await self.application.objects.create( User, **registerForm.data) ret_data["id"] = user.id else: self.set_status(400) for field in registerForm.erros: ret_data[field] = registerForm[field][0] return self.finish(ret_data)
def register(self, request): form = RegisterForm(data = request.POST if request.POST else None) if form.is_valid(): form.save() return HttpResponseRedirect(request.GET.get('back', '/blog/')) return {'form': form}