def register(request): context = {} if request.method == 'GET': form = RegisterForm() context['form'] = form if request.method == 'POST': form = RegisterForm(request.POST) if form.is_valid(): username = form.cleaned_data['name'] email = form.cleaned_data['email'] if User.objects.filter(username=username) or User.objects.filter( email=email): form = RegisterForm() form.errors['注意:'] = (u"用户已存在,请更换用户名或邮箱注册") else: user = User() user.username = username user.email = email user.set_password(form.cleaned_data['password']) user.save() p = UserProfile(belong_to=user) p.name = username p.email = email p.save() return redirect(to='login') context['form'] = form return render(request, "register.html", context)
def post(self, request): register_form = RegisterForm(request.POST) if register_form.is_valid(): username = register_form.cleaned_data["username"] user = UserProfile.objects.filter(username=username).first() if user: err_allow = u"用户名已经存在" return render(request, "usercenter/register.html", locals()) else: user = UserProfile() user.username = username user.password = make_password( register_form.cleaned_data["password1"]) user.email = register_form.cleaned_data["email"] user.gender = 0 user.save() login(request, user) path = request.GET.get('next') if path == 'None': return redirect(reverse("blog:index")) return redirect(path) else: return render(request, "usercenter/register.html", locals())