def user_reg(request): if request.method == "GET": return render(request, 'users/reg.html') else: user_register_form = UserRegForm(request.POST) if user_register_form.is_valid(): username = user_register_form.cleaned_data['username'] password = user_register_form.cleaned_data['password'] email = user_register_form.cleaned_data['email'] user = UserProfile.objects.filter(username=username) is_email = UserProfile.objects.filter(email=email) if user: return render(request, 'users/reg.html', {'msg': '用户名已存在'}) elif is_email: return render(request, 'users/reg.html', {'msg': '此邮箱已注册'}) else: a = UserProfile() a.username = username a.email = email a.password = password a.set_password(password) a.save() return redirect(reverse('users:login')) else: return render(request, 'users/reg.html', {'user_register_form': user_register_form})
def test__not_valid_forms(self): not_valid_data = { 'username': '******', 'email': '*****@*****.**', } form = UserRegForm(data=not_valid_data) self.assertFalse(form.is_valid())
def register(request): data = UserRegForm(request.POST) # 验证之后的值 if not data.is_valid(): # 验证是否合法 return HttpResponse('erorr') res = data.cleaned_data # 调用is_Valid后会给一个cleaned_data,是合法数据 User.objects.create_user(**res) url_source = request.META['HTTP_REFERER'] return redirect(url_source)
def register(request): data = UserRegForm(request.POST) if not data.is_valid(): return HttpResponse('数据不合法') res = data.cleaned_data # 表里面创建用户 user = User.objects.create_user(**res) user_login(request, user) url_source = request.META['HTTP_REFERER'] return redirect(url_source)
def test_valid_forms(self): valid_data = { 'username': '******', 'email': '*****@*****.**', 'password1': 'qwerty212', 'password2': 'qwerty212', } form = UserRegForm(data=valid_data) self.assertTrue(form.is_valid())
def docReg(request): user_form_class = UserRegForm doc_form_class = DoctorRegForm user_form = user_form_class(request.POST or None) doc_form = doc_form_class(request.POST or None) if request.method == "POST": if user_form.is_valid() and doc_form.is_valid(): user = user_form.save(commit=False) user.save() user.doctor.email = doc_form.clean_data.get("email") user.doctor.hpname = doc_form.clean_data.get("hpname") user.doctor.specialty = doc_form.clean_data.get("specialty") user.doctor.password1 = doc_form.clean_data.get("password1") user.doctor.password2 = doc_form.clean_data.get("password2") user.doctor.save() else: user_form = UserRegForm(prefix="UF") doc_form = DoctorRegForm(prefix="DF") return render( request, "registration/d_reg.html", { "user_form": user_form, "doc_form": doc_form, }, )
def get_context_data(self, **kwargs): # 获取分类表中信息 category = Category.objects.order_by('id') # 实例化表单 login_form = UserLoginForm() reg_form = UserRegForm() products = Product.objects.order_by('-id') # 返回信息 kwargs['products'] = products kwargs['login_form'] = login_form kwargs['reg_form'] = reg_form kwargs['comment_form'] = CommentForm() kwargs['comments'] = Comments.objects.filter( product=kwargs['object'].id).order_by("-time") kwargs['category'] = category return super().get_context_data(**kwargs)
def get_context_data(self, **kwargs): # 获取产品表中的产品 products = Product.objects.order_by('-id') # 获取分类表中信息 category = Category.objects.order_by('id') # 产品表中的分类信息sql MobilePhones = Product.objects.filter( kind__name="Mobile Phones").order_by('id') Desktop = Product.objects.filter(kind__name="Desktop").order_by('id') Laptop = Product.objects.filter(kind__name="Laptop").order_by('id') Accessories = Product.objects.filter( kind__name="Accessories").order_by('id') Software = Product.objects.filter(kind__name="Software").order_by('id') addtime = Product.objects.filter( kind__name="addtime").order_by('-time') # 产品表中获得特色产品 featureproduct = Product.objects.filter(featureproduct=True) # 实例化表单 login_form = UserLoginForm() reg_form = UserRegForm() # 返回信息 kwargs['products'] = products kwargs['category'] = category kwargs['MobilePhones'] = MobilePhones kwargs['Desktop'] = Desktop kwargs['Laptop'] = Laptop kwargs['Accessories'] = Accessories kwargs['Software'] = Software kwargs['addtime'] = addtime kwargs['featureproduct'] = featureproduct kwargs['login_form'] = login_form kwargs['reg_form'] = reg_form return super().get_context_data(**kwargs)