def registration(request): if request.method == "POST": form = RegForm(request.POST, request.FILES) if form.is_valid(): if form.cleaned_data["password"] != form.cleaned_data[ "confirm_password"]: return render(request, "register.html", { "form": form, "errors": ["Incorrect confirmpassword"] }) else: user = User.objects.create_user( username=form.cleaned_data["username"], email=form.cleaned_data["email"], password=form.cleaned_data["password"], first_name=form.cleaned_data["first_name"], last_name=form.cleaned_data["last_name"]) user.profile_pic = form.cleaned_data["profile_pic"] user = authenticate(username=form.cleaned_data["username"], password=form.cleaned_data["password"]) login(request, user) return redirect(reverse("index")) else: return render(request, "register.html", {"form": form}) else: return render(request, "register.html", {"form": RegForm()})
def register(request): if request.method == 'POST': reg_form = RegForm(request.POST) if reg_form.is_valid(): username = reg_form.cleaned_data['username'] email = reg_form.cleaned_data['email'] password = reg_form.cleaned_data['password'] # 创建用户 user = User.objects.create_user(username=username, email=email, password=password) user.save() # 登录用户 user = auth.authenticate(request, username=username, password=password) auth.login(request, user) return redirect(request.GET.get("from", reverse('home'))) else: reg_form = RegForm() # request方法不是POST时 context = {} # if分支的共有部分,都会执行以下代码。 context['reg_form'] = reg_form return render(request, 'register.html', context)
def register(request): if request.method == 'POST': register_form = RegForm(request.POST) if register_form.is_valid(): username = register_form.cleaned_data['username'] email = register_form.cleaned_data['email'] password = register_form.cleaned_data['password'] user = User.objects.create_user(username, email, password) user.save() user = auth.authenticate(username=username, password=password) auth.login(request, user) return redirect(request.GET.get('from', reverse('index'))) else: register_form = RegForm() return render(request, 'user/register.html', locals())
def reg(request): """注册界面""" if request.method == "POST": # 接收数据 res = request.POST # 判断用户是否勾选用户协议 if res.get("checkbox"): # 判断手机号是否已经存在数据库中 if User.objects.filter(telephone=res.get("telephone")): context = { "a": "手机号已经被注册" } return render(request, "user/reg.html", context) else: # 创建form对象,验证form表单中的数据是否写好 form = RegForm(res) # 判断是否合法性 if form.is_valid(): # 开始验证 # 处理数据,将清洗后数据保存到数据库 aa = form.cleaned_data # 获取表单密码 password = aa.get("password") # 密码加密成哈希 password = set_password(password) # 将手机号和加密后的密码创建到数据库 User.objects.create(telephone=aa.get("telephone"), password=password) # 跳转到登录界面 return redirect('user:登录') else: # 错误,将错误信息显示到页面 context = { "errors": form.errors, "res": res } return render(request, "user/reg.html", context) else: context = { "c": "请同意用户协议" } return render(request, "user/reg.html", context) else: return render(request, "user/reg.html")
def register(request): if request.method == 'POST': reg_form = RegForm(request.POST, request=request) if reg_form.is_valid(): username = reg_form.cleaned_data['username'] email = reg_form.cleaned_data['email'] password = reg_form.cleaned_data['password'] # 创建用户 user = User.objects.create_user(username, email, password) user.save() # 清除session del request.session['register_code'] # 登录用户 user = auth.authenticate(username=username, password=password) auth.login(request, user) return redirect(request.GET.get('from', reverse('home'))) else: reg_form = RegForm() return render(request, 'login/register.html', {'reg_form': reg_form})
def register(request): if request.method == 'POST': email_code = request.POST.get('email_code').upper() reg_form = RegForm(request.POST) if email_code == request.session.get('bind_email_code', '').upper(): if reg_form.is_valid(): # 创建新用户 # 方法一 # form_obj.cleaned_data.pop('re_password') # models.UserProfile.objects.create_user(**form_obj.cleaned_data) # 方法二 obj = reg_form.save() obj.set_password(obj.password) obj.save() del request.session['bind_email_code'] return redirect(request.GET.get('next', reverse('login'))) else: context = {} context['reg_form'] = reg_form context['error'] = '验证码错误' return render(request, 'register.html', context) else: reg_form = RegForm() context = {} context['reg_form'] = reg_form return render(request, 'register.html', context)
def register(request): if request.method == "POST": reg_form = RegForm(request.POST) if reg_form.is_valid(): # 创建用户 username = reg_form.cleaned_data['username'] email = reg_form.cleaned_data['email'] password = reg_form.cleaned_data['password'] password_again = reg_form.cleaned_data['password_again'] user = User.objects.create_user(username, email, password) user.save() #登陆用户 #user = auth.authenticate(user=user, password=password) print(user) auth.login(request, user) return redirect(request.GET.get('from', reverse('home'))) else: reg_form = RegForm() context = {} context['reg_form'] = reg_form return render(request, '../user/templates/register.html', context)