def post(self, request, *args, **kwargs): register_post_form = RegisterPostForm(request.POST) # 校验手机号(是否已经注册),动态验证码,密码 banners = Banner.objects.all()[:3] if register_post_form.is_valid(): # 获取手机号码和密码 mobile = register_post_form.cleaned_data['mobile'] password = register_post_form.cleaned_data['password'] # 新建一个用户 # 默认用户名=手机号 user = UserProfile(username=mobile) user.set_password(password) # 别忘记存手机号 user.mobile = mobile # 最后对新建的对象进行保存,否则不会存进数据库 user.save() # 注册成功后,跳转首页 login(request, user) return HttpResponseRedirect(reverse('index')) # 若验证失败 else: register_get_form = RegisterForm() return render( request, 'register.html', { 'register_get_form': register_get_form, 'register_post_form': register_post_form, 'banners': banners, })
def post(self, request): register_post_form = RegisterPostForm(request.POST) if register_post_form.is_valid(): user_name = request.POST.get('mobile', '') password = request.POST.get('password', '') user = UserProfile() user.username = user_name user.mobile = user_name user.set_password(password) user.save() # 写入欢迎注册消息 user_message = UserMessage() user_message.user = user user_message.message = '欢迎注册!!!!!' user_message.save() # send_register_email(user_name, 'register') login(request, user) return HttpResponseRedirect(reverse('index')) else: register_get_form = RegisterGetForm() return render( request, 'register.html', { 'register_get_form': register_get_form, 'register_post_form': register_post_form })
def post(self, request): """注册提交""" # 校验数据 register_captcha = RegisterForm(request.POST) register_post = RegisterPostForm(request.POST) # 取出一个错误 error_val = '' error_key = '' for key, val in register_post.errors.items(): error_val = val[0] error_key = key break if not register_post.is_valid(): return render(request, 'register.html', { 'register_captcha': register_captcha, 'register_post': register_post, 'error_val': error_val, 'error_key': error_key, }) # 业务逻辑处理:注册用户 mobile = register_post.cleaned_data['mobile'] password = register_post.cleaned_data['password'] # 注册用户 user = UserProfile() user.username = mobile user.mobile = mobile user.set_password(password) user.save() # 验证通过,返回到登录页面 return redirect(reverse('users:login'))
def get(self,request,*args,**kwargs): captcha_form=RegisterPostForm() current_page='info' context={ 'captcha_form':captcha_form, 'current_page':current_page } return render(request,'usercenter-info.html',context)
def post(self,request,*args,**kwargs): dynamic_login = True register_post_form = RegisterPostForm(request.POST) if register_post_form.is_valid(): mobile = register_post_form.cleaned_data['mobile'] password = register_post_form.cleaned_data['password'] # 新建用户 user = UserProfile(username=mobile) user.set_password(password) user.mobile = mobile user.save() login(request, user) return HttpResponseRedirect(reverse('index')) else: # 验证码错误 register_get_form=RegisterGetForm() context={'register_get_form':register_get_form,'register_post_form':register_post_form} return render(request, 'register.html', context=context)
def post(self, request, *args, **kwargs): register_post_form = RegisterPostForm(request.POST) if register_post_form.is_valid(): mobile = register_post_form.cleaned_data["mobile"] password = register_post_form.cleaned_data["password"] # 新建一个用户 user = UserProfile(username=mobile) user.set_password(password) user.mobile = mobile user.save() login(request, user) return HttpResponseRedirect(reverse("index")) else: register_get_form = RegisterGetForm() return render(request, "register.html", { "register_get_form":register_get_form, "register_post_form": register_post_form })
def post(self, request, *args, **kwargs): banners = Banner.objects.all()[:3] register_post_form = RegisterPostForm(request.POST) if register_post_form.is_valid(): mobile = register_post_form.cleaned_data['mobile'] password = register_post_form.cleaned_data['password'] user = UserProfile(username=mobile) user.set_password(password) user.mobile = mobile user.save() login(request, user) return HttpResponseRedirect(reverse('index')) else: register_get_form = RegisterGetForm() return render( request, 'register.html', { 'register_get_form': register_get_form, 'register_post_form': register_post_form, 'banners': banners })
def post(self, request, *args, **kwargs): register_post_form = RegisterPostForm(request.POST) if register_post_form.is_valid(): # 没有注册账号则注册 mobile = register_post_form.cleaned_data['mobile'] password = register_post_form.cleaned_data['password'] # 新建一个用户 user = UserProfile(username=mobile) user.set_password(password) user.mobile = mobile user.save() auth.login(request, user) return HttpResponseRedirect(reverse('index')) else: register_get_form = RegisterGetForm() return render( request, 'register.html', { 'register_post_form': register_post_form, 'register_get_form': register_get_form })
def post(self, request, *args, **kwargs): #处理用户传递过来的请求 register_post_form = RegisterPostForm(request.POST) dynamic_login = True if register_post_form.is_valid(): # 注册账号 mobile = register_post_form.cleaned_data["mobile"] password = register_post_form.cleaned_data["password"] # 新建一个用户 user = UserProfile(username=mobile) user.set_password(password) # 生成该密码的秘文 user.mobile = mobile user.save() login(request, user) # 使该用户登陆 return HttpResponseRedirect(reverse("index")) # 跳转到首页 else: register_get_form = RegisterGetForm() #新的验证码 # register_post_form = RegisterPostForm() #错误的信息 return render(request, "register.html", { "register_get_form": register_get_form, "register_post_form": register_post_form, }) # 传递到html文件中
def post(self, request, *args, **kwargs): register_post_form = RegisterPostForm(request.POST) if register_post_form.is_valid(): email = register_post_form.cleaned_data["email"] password = register_post_form.cleaned_data["password"] # 新建一个用户 user = UserProfile(username=email) #username必须唯一,比如如果数据库存在该username,就会出现无法插入数据库记录错误。 user.set_password(password) user.email = email user.save() login(request, user) new_message = "欢迎使用FreeStudy,祝您学习愉快!" messages = UserMessage() messages.user = request.user messages.message = new_message messages.save() return HttpResponseRedirect(reverse("index")) #注册成功返回主页 else: register_get_form = RegisterGetForm() return render(request, "register.html", { "register_get_form":register_get_form, "register_post_form": register_post_form })
def post(self, request, *args, **kwargs): #form表单实例化的时候、如果是post请求、需要加上request.POST register_post_form = RegisterPostForm(request.POST) #如果验证成功、则说明手机号是新的、验证码也和redis当中的一样 if register_post_form.is_valid(): mobile = register_post_form.cleaned_data["mobile"] password = register_post_form.cleaned_data["password"] print("验证成功以后会打印这句话") #手机号是用户名 user = UserProfile(username=mobile) #设置密码 加密的形式 user.set_password(password) #手机号是手机号 user.mobile = mobile #保存 user.save() #记录登录 login(request, user) #跳转到首页 return HttpResponseRedirect(reverse("index")) else: #如果验证失败的话 重新分配图片验证码到 register_get_form表单 register_get_form = RegisterGetForm() return render( request, "register.html", { "register_get_form": register_get_form, #这个里面有错误信息、传递过去显示出来 "register_post_form": register_post_form })