def reg(request): if request.user.is_authenticated: return redirect('blog:index') else: title = '注册' if request.method == 'POST': reg_form = RegForm(request.POST) if reg_form.is_valid(): username = reg_form.cleaned_data['username'] first_name = reg_form.cleaned_data['first_name'] last_name = reg_form.cleaned_data['last_name'] email = reg_form.cleaned_data['email'] phone = reg_form.cleaned_data['phone'] wechat = reg_form.cleaned_data['wechat'] password = reg_form.cleaned_data['password'] user = User.objects.create(username=username, first_name=first_name, last_name=last_name, email=email, phone=phone, wechat=wechat, password=password) user.save() return redirect('blog:login') else: error = '请检查表单,全部填写' return render(request, 'auth/reg.html', locals()) return render(request, 'auth/reg.html', locals())
def do_reg(request): try: if request.method == 'POST': reg_form = RegForm(request.POST) if reg_form.is_valid(): # 注册 user = User.objects.create( username=reg_form.cleaned_data["username"], email=reg_form.cleaned_data["email"], url=reg_form.cleaned_data["url"], password=make_password(reg_form.cleaned_data["password"]), ) user.save() # 登录 user.backend = 'django.contrib.auth.backends.ModelBackend' # 指定默认的登录验证方式 login(request, user) return redirect(request.POST.get('source_url')) else: return render(request, 'failure.html', {'reason': reg_form.errors}) else: reg_form = RegForm() except Exception as e: logger.error(e) return render(request, 'reg.html', locals())
def reg(request): if request.is_ajax(): regForm = RegForm(request.POST) regResponse = {"user": None, "error_msg": None} if regForm.is_valid(): username = regForm.cleaned_data.get("username") password = regForm.cleaned_data.get("password") email = regForm.cleaned_data.get("email") avatar = request.FILES.get("avatar") print(regForm.cleaned_data, "------") user = UserInfo.objects.create_user(username=username, password=password, email=email, avatar=avatar) regResponse["user"] = user.username else: regResponse["error_msg"] = regForm.errors # errors只存错误字段 return HttpResponse(json.dumps(regResponse)) regForm = RegForm() return render(request, 'reg.html', locals())
def reg(request): reg_form=RegForm() if request.method=='POST' and ('reg_form' in request.POST): reg_form=RegForm(request.POST, request.FILES) if reg_form.is_valid(): image=request.FILES['avatar'] reg_form.create_user(image) option_mes='Thanks for registration!' return HttpResponseRedirect(reverse('home_page')) return render_to_response('auth_form.html',{'reg_form':reg_form},context_instance=RequestContext(request))
def regCnblog(request): from blog.forms import RegForm # from django.contrib.auth.models import User # 自定义了User,需将auth的替换成自己的 from blog.models import UserInfo as User # 自定义了User,需将auth的替换成自己的 regForm = RegForm() if request.is_ajax(): regForm = RegForm(request.POST) # 根据ajax的data实例化 regResponse = {"user": None, "errormsg": None} # 注册状态字典表 #print("----",request.POST) if regForm.is_valid(): # 注册成功 print('---regForm.cleaned_data--', regForm.cleaned_data) print('---regForm.cleaned_data-username-', regForm.cleaned_data.get("username")) username = regForm.cleaned_data.get("username") password = regForm.cleaned_data.get("password") email = regForm.cleaned_data.get("email") avatar = request.FILES.get("upFile") if not avatar: #如果用户没有选择图片,则用默认图片,这个放在前端,后端不好实现 avatar = "avatarDir/AIF.jpg" else: with open("static/pictures/%s.jpg" % username, 'wb') as f: for line in avatar: f.write(line) # 生成新用户,修改状态表 newuser = User.objects.create_user(username=username, password=password, email=email, avatar=avatar) if newuser: regResponse["user"] = username else: raise EOFError("创建新用户失败") else: # 注册失败 print('---regForm.cleaned_data--', regForm.cleaned_data) print('---regForm.errors--', regForm.errors) regResponse["errormsg"] = regForm.errors return HttpResponse(json.dumps(regResponse)) # print("------",request.POST,request.POST.get("username")) else: # 正常访问 return render(request, 'reg.html', {"regForm": regForm}) # 字典也可写错 locals(),名字要一致
def register(): if current_user.is_authenticated: return redirect(url_for('hello')) form=RegForm() if form.validate_on_submit(): hashed_password=str(bcrypt.generate_password_hash(form.password.data)) hashed_password=str(hashed_password) print(type(hashed_password)) user=User(username=form.username.data,email=form.email.data,password=hashed_password) db.session.add(user) db.session.commit() flash('Your account has been created. You can now login!','success') return redirect(url_for('login')) return render_template('register.html',form=form,title='Register')
def resgister(request): res = {'status': 1, 'msg': None} if request.method == 'POST': obj = RegForm(request.POST) avatar = request.FILES.get('avatar') if obj.is_valid(): obj.cleaned_data.pop('re_password') models.UserInfo.objects.create_user(**obj.cleaned_data, avatar=avatar) res['msg'] = "/login/" else: res['status'] = 0 res['msg'] = obj.errors return HttpResponse(json.dumps(res)) obj1 = RegForm() return render(request, 'resgister.html', {'obj': obj1})
def do_reg(request): try: if request.method == 'POST': reg_form = RegForm(request.POST) if reg_form.is_valid(): # 注册 user = User.objects.create(username=reg_form.cleaned_data["username"], email=reg_form.cleaned_data["email"], url=reg_form.cleaned_data["url"], password=make_password(reg_form.cleaned_data["password"]), ) user.save() # 登录 # 指定默认的登录验证方式 user.backend = 'django.contrib.auth.backends.ModelBackend' login(request, user) return redirect(request.POST.get('source_url')) else: return render(request, 'failure.html', {'reason': reg_form.errors}) else: reg_form = RegForm() except Exception as e: logger.error(e) return render(request, 'reg.html', locals())
def post(self, request): res = {"code": 0} # print(request.POST) v_code = request.POST.get('v_code', '') # 首先验证验证码是否有误 if v_code.upper() == request.session.get('v_code', ''): form_obj = RegForm(request.POST) # 使用form做校验 if form_obj.is_valid(): # 去除数据库不需要的数据 form_obj.cleaned_data.pop('re_password') # 取到用户的头像文件 avatar_file = request.FILES.get('avatar') # 数据库保存用户信息 models.UserInfo.objects.create_user(**form_obj.cleaned_data, avatar=avatar_file) res['msg'] = '/login/' else: res['code'] = 1 res['msg'] = form_obj.errors else: res['code'] = 2 res['msg'] = '验证码错误' return JsonResponse(res)
def reg(request): form_obj = RegForm() if request.method == 'POST': form_obj = RegForm(request.POST) if form_obj.is_valid(): # 创建新用户 form_obj.cleaned_data.pop('re_password') user = form_obj.save() user.set_password(user.password) user.save() return redirect(reverse('blog_login')) return render(request, 'blog/reg.html', {'form_obj': form_obj})
def get(self, request): form_obj = RegForm() return render(request, 'register.html', {'form_obj': form_obj})