def user_register(request): if request.method == 'GET': user_register_form = UserRegisterForm() return render(request, 'register.html', {'user_register_form': user_register_form}) else: user = UserRegisterForm(request.POST) if user.is_valid(): username = user.cleaned_data['username'] email = user.cleaned_data['email'] password = user.cleaned_data['password'] u = UserProfile.objects.filter( Q(username=username) | Q(email=email)) if u: return render(request, 'register.html', {'msg': '此用户名或邮箱已被注册'}) else: user_profile = UserProfile() user_profile.email = email user_profile.username = username user_profile.set_password(password) user_profile.is_active = 0 user_profile.save() send_email(email, 'register') return HttpResponse( '注册成功,去激活吧! <a href="http://123.56.23.97">回到首页</a>') else: return render(request, 'register.html', {'user_register_form': user})
def user_register(request): if request.method == 'GET': return render(request, 'user/register.html') else: rform = UserRegisterForm(request.POST) # 使用form获取数据 print('--------------------------', rform) if rform.is_valid(): # 进行数据的校验 # 从干净的数据中取值 username = rform.cleaned_data.get('username') email = rform.cleaned_data.get('email') mobile = rform.cleaned_data.get('mobile') password = rform.cleaned_data.get('password') if not UserProfile.objects.filter( Q(username=username) | Q(mobile=mobile)).exists(): # 注册到数据库中 password = make_password(password) # 密码加密 user = UserProfile.objects.create(username=username, password=password, email=email, mobile=mobile) if user: return render(request, 'user/login.html') else: return render(request, 'user/register.html', context={'msg': '用户名或者手机号码已经存在!'}) return render(request, 'user/register.html', context={ 'msg': '注册失败,重新填写!', 'errors': rform.errors })
def register_action(request): context = {} if request.method == "GET": return render(request, 'register.html') form = UserRegisterForm(request.POST) context["registerForm"] = form if not form.is_valid(): print("User Registration Form is invalid ...") return render(request, 'register.html', context) print("User Registration Form is valid.") username, email = form.cleaned_data['username'], form.cleaned_data['email'] password = form.cleaned_data['password1'] user = User.objects.create_user(username=username, email=email, password=password) user.save() profile = UserProfile(user=user, good=0, fair=0, bad=0) profile.save() cart = ShoppingCart(user=user) cart.save() sendEmail(email) # send registration email print("New user is created but not activated.") return redirect('/login')
def test_admin_create_instructor_account_valid(self): # Valid form entry by entering the correct parameters for each field username = "******" email = "*****@*****.**" passwrd1 = "bryansucksalot" passwrd2 = "bryansucksalot" form = UserRegisterForm( data={"username": username, "email": email, "password1": passwrd1, "password2": passwrd2, "role": 2} ) self.assertTrue(form.is_valid())
def test_admin_create_instructor_account_invalid1(self): # Invalid form because the password is not long enough for requirements username = "******" email = "*****@*****.**" passwrd1 = "bry" passwrd2 = "bry" form = UserRegisterForm( data={"username": username, "email": email, "password1": passwrd1, "password2": passwrd2, "role": 2} ) self.assertFalse(form.is_valid())
def test_admin_create_instructor_account_invalid3(self): # Invalid form because the password is too commonly used username = "******" email = "*****@*****.**" passwrd1 = "password" passwrd2 = "password" form = UserRegisterForm( data={"username": username, "email": email, "password1": passwrd1, "password2": passwrd2, "role": 2} ) self.assertFalse(form.is_valid())
def register_post(request): """只接受post请求的注册接口""" if request.method == 'POST': user = UserRegisterForm(request.POST) # print(user) if not user.is_valid(): # valid = user.is_valid() return JsonResponse(user.errors.get_josn_data, safe=False) user = UserLoginForm() return render(request, 'user/register_post.html', {'user': user})
def create(request): if request.method == 'POST': form = UserRegisterForm(request.POST) if form.is_valid(): form.save() messages.success(request, 'User registration by admin is successfully!') role = Role(user_id=User.objects.latest('id').id, role=request.POST.get('role')) role.save() return redirect('setting-index') return render(request, 'user/add.html', {'title': 'New User'})
def register(request): if request.method == 'GET': return render(request, 'register.html') if request.method == 'POST': form = UserRegisterForm(request.POST) if form.is_valid(): password = make_password(form.cleaned_data.get('password')) # 实现注册 User.objects.create(username=form.cleaned_data.get('username'), password=password) return HttpResponseRedirect(reverse('user:login')) else: return render(request, 'register.html', {'errors': form.errors})
def form_register(request): if request.method == 'GET': return render(request, 'register.html') if request.method == 'POST': data = request.POST form = UserRegisterForm(data) if form.is_valid(): User.objects.create(name=form.cleaned_data.get('name'), password=form.cleaned_data.get('pw')) return HttpResponseRedirect(reverse('user:login')) else: errors = form.errors return render(request, 'register.html', {'error': errors})
def register(request): if request.method == 'GET': return render(request,'register.html') if request.method == 'POST': data = request.POST form = UserRegisterForm(data) if form.is_valid(): pwd = form.cleaned_data.get('pwd') password = make_password(pwd) user_name = form.cleaned_data.get('user_name') email = form.cleaned_data.get('email') User.objects.create(username=user_name,password=password,email=email) return HttpResponseRedirect(reverse('user:login')) else: return render(request,'register.html',{'errors':form.errors})
def register(request): if request.method == 'GET': return render(request, 'register.html') if request.method == 'POST': # 将页面中提交的参数交给form表单做验证 form = UserRegisterForm(request.POST) if form.is_valid(): # 检验通过,对密码进行加密 password = make_password(form.cleaned_data.get('password')) User.objects.create(username=form.cleaned_data.get('username'), password=password, email=form.cleaned_data.get('email')) return HttpResponseRedirect(reverse('user:login')) else: # 验证不通过 return render(request, 'register.html', {'errors': form.errors})
def register(request): if request.method == 'GET': return render(request, 'register.html') if request.method == 'POST': form = UserRegisterForm(request.POST) if form.is_valid(): username = form.cleaned_data['user_name'] password = form.cleaned_data['pwd'] email = form.cleaned_data['email'] new_password = make_password(password) User.objects.create(username=username, password=new_password, email=email) return HttpResponseRedirect(reverse('user:login')) else: errors = form.errors return render(request, 'register.html', {'errors': errors})
def register(request): if request.method == 'GET': return render(request, 'register.html') if request.method == 'POST': form = UserRegisterForm(request.POST) if form.is_valid(): User.objects.create( username=form.cleaned_data['username'], password=make_password(form.cleaned_data['password']), user_id='user_' + str(int(time.time())), ) return HttpResponseRedirect(reverse('home:login')) else: msg = form.errors msg = str(sorted(msg.items())[0][1].as_text()) return render(request, 'register.html', {'form': msg})
def register(request): if request.method == 'GET': return render(request, 'register.html') if request.method == 'POST': data = request.POST form = UserRegisterForm(data) if form.is_valid(): # form表单数据无问题 # 密码加密 password = make_password(form.cleaned_data.get('pwd')) # 注册,在数据库中增加用户 User.objects.create(username=form.cleaned_data.get('username'), password=password, email=form.cleaned_data.get('email')) return HttpResponseRedirect(reverse('user:login')) else: # 有错,返回错误 return render(request, 'register.html', {'errors': form.errors})
def register(request): if request.method == 'GET': # 如果请求为GET,返回注册页面 return render(request, 'register.html') if request.method == 'POST': # 校验参数 form = UserRegisterForm(request.POST) # 判断is_valid()是否为True if form.is_valid(): # 注册.使用make_password进行密码加密,否则为明文 password = make_password(form.cleaned_data['password']) User.objects.create(username=form.cleaned_data['username'], password=password, email=form.cleaned_data['email']) # 跳转到登录页面 return HttpResponseRedirect(reverse('user:login')) else: return render(request, 'register.html', {'form': form})
def register(): form = UserRegisterForm() if request.method == 'GET': return render_template('register.html', form=form) if request.method == 'POST': # 验证提交的字段信息 if form.validate(): username = form.username.data password = form.password.data # 实现注册,保存用户信息到User模型中 user = User() user.username = username user.password = generate_password_hash(password) db.session.add(user) db.session.commit() return redirect(url_for('user.login')) else: # 验证失败,from.errors中存在错误信息 return render_template('register.html', form=form)
def register(request): if request.method == 'GET': return render(request, 'register.html') if request.method == 'POST': form = UserRegisterForm(request.POST) if form.is_valid(): # 字段验证成功, 用户名不存在数据库两次输入的密码一致 username = form.cleaned_data['user_name'] password = form.cleaned_data['pwd'] email = form.cleaned_data['email'] # from django.contrib.auth.hashers import make_password new_password = make_password(password) User.objects.create(username=username, password=new_password, email=email) return HttpResponseRedirect(reverse('user:login')) else: # 验证失败返回错误信息 errors = form.errors return render(request, 'register.html', {'errors': errors})
def register(request): # 注册功能 if request.method == "POST": data = request.POST form = UserRegisterForm(data) if form.is_valid(): data = form.cleaned_data password = data.get("password2") # 调用方法对密码加密 password = set_password(password) # 将数据写入数据库 UserModel.objects.create(phone=data.get("phone"), password=password) # 跳转到登陆页面 return redirect("user:login") else: context = {"errors": form.errors, "phone": data.get("phone")} return render(request, 'user/reg.html', context) else: return render(request, 'user/reg.html')
def register(request): if request.method == "GET": # print('GET成功') return render(request, 'register.html') if request.method == "POST": data = request.POST form = UserRegisterForm(data) if form.is_valid(): User.objects.create_user(username=form.cleaned_data.get('name'), password=form.cleaned_data.get('pw')) user = User.objects.get(username='******') return render(request, 'index.html', {'user': user}) # return HttpResponseRedirect(reverse('user:login')) else: errors = form.errors return render(request, 'register.html', {'error': errors})
def registro(request): data = {'form': UserRegisterForm()} if request.method == 'POST': formulario = UserRegisterForm(data=request.POST) if formulario.is_valid(): nivel = formulario.cleaned_data.get("groups") u = formulario.save() group = Group.objects.get(name=nivel) group.user_set.add(u.id) # user = authenticate(username=formulario.cleaned_data["username"], password=formulario.cleaned_data["password1"]) # login(request, user) messages.add_message(request, messages.SUCCESS, "Usuario creado correctamente") return redirect(to="usuarios") data["form"] = formulario return render(request, 'registration/registro.html', data)
def form_register(request): if request.method == 'GET': return render(request, 'register.html') if request.method == 'POST': data = request.POST # data: {'name':'111', 'pw': '222', 'pw2':'3333'} # 将页面中提交的参数交给form表单做验证 form = UserRegisterForm(data) if form.is_valid(): # 验证通过 # 密码加密 password = make_password(form.cleaned_data.get('pw')) # 注册账号 User.objects.create(name=form.cleaned_data.get('name'), password=password) return HttpResponseRedirect(reverse('user:index')) else: # 验证不同通过,可以从form中获取错误的信息 errors = form.errors return render(request, 'register.html', {'errors': errors})
def post(self, request): # 1.接受参数 data = request.POST # 2.验证参数的合法性 # >>>>1.创建一个form表单验证的对象,来进行数据合法性的验证 form = UserRegisterForm(data) if form.is_valid(): # 在数据合法的情况下执行 # 接受清洗后的数据 # 1.<<<< 接收手机号码 phone = form.cleaned_data.get("phone") # 2.<<<< 接收密码 password = form.cleaned_data.get("password") # 3.将密码加密 password = set_password(password) # 将接受的数据,保存到数据当中 UserModel.objects.create(phone=phone, password=password) # 保存到数据库成功 return redirect("user:用户登录") else: # 在数据不合法的情况下 return render(request, 'user/register.html', {"form": form})
def register(request): if request.method == 'POST': form = UserRegisterForm(request.POST) if form.is_valid(): form.save() username = form.cleaned_data.get('username') messages.success(request, f'Account created for {username}!') return redirect('blog-home') else: form = UserRegisterForm() return render(request, 'users/register.html', {'form': form})
def UserRegister(request): # 获取POST参数 username = request.POST.get('username') password = request.POST.get('password') nickname = request.POST.get('nickname') tel = request.POST.get('tel') img_code = request.POST.get('img_code') # 调用Form验证 form_user = UserRegisterForm({ 'username': username, 'password': password, 'nickname': nickname, 'tel': tel, }) if form_user.is_valid(): if User.objects.filter(username=username).only('username').first(): return JsonResponse(USERNAME_EXIST) # 创建用户 user = User() user.username = username user.password = to_md5_hex(password) user.nickname = nickname user.tel = tel now_time = timezone.now().strftime('%Y-%m-%d %H:%M:%S') user.lastvisit = now_time user.save() # 记录用户登录信息 new_login_log = LoginLog() new_login_log.user = user new_login_log.ipaddr = get_ip_address(request) new_login_log.logdate = now_time new_login_log.save() REGISTER_SUCCESS['token'] = uuid1().hex redis_client = get_redis_connection() redis_client.set(LOGIN_SUCCESS['token'], pickle.dumps(user), 3600) return JsonResponse(REGISTER_SUCCESS) else: FORM_ERRORS['msg'] = form_user.errors return JsonResponse(FORM_ERRORS)
def register(request): if request.method == "POST": form = UserRegisterForm(request.POST) if form.is_valid(): form.save() username = form.cleaned_data.get('username') messages.success(request, f'Account has been created! You can login now') return redirect('login') else: form = UserRegisterForm() return render(request, 'user/register.html', {'form': form})
def register(request): if request.method == 'GET': return render(request, 'register.html') if request.method == 'POST': # 定义表单验证页面传递的参数 form = UserRegisterForm(request.POST) if form.is_valid(): # 校验成功 user = User() user.account_number = form.cleaned_data['account_number'] user.password = form.cleaned_data['password'] user.name = form.cleaned_data['name'] user.sex = form.cleaned_data['sex'] user.id_card = form.cleaned_data['id_card'] user.age = form.cleaned_data['age'] user.phone = form.cleaned_data['phone'] user.address = form.cleaned_data['address'] user.save() success = '注册成功' return render(request, 'register.html', {'success': success}) errors = form.errors return render(request, 'register.html', {'errors': errors})
def register(request): if request.method == 'GET': return render(request, 'register.html') if request.method == 'POST': # 使用表单做验证 form = UserRegisterForm(request.POST) if form.is_valid(): # 字段验证成功,注册用户名不存在于数据库,密码和确认密码是一致的 username = form.cleaned_data['user_name'] password = form.cleaned_data['pwd'] email = form.cleaned_data['email'] # from django.contrib.auth.hashers import make_password new_password = make_password(password) User.objects.create(username=username, password=new_password, email=email) # 注意render和redirect的区别 return HttpResponseRedirect(reverse('user:login')) else: # 字段验证不成功,将验证失败的信息返回给页面 errors = form.errors return render(request, 'register.html', {'errors': errors})
def user_zhuce(request): if request.method == 'GET': rform = RegisterForm() return render(request, 'user/zhuce.html', context={'rform': rform}) else: rform = UserRegisterForm(request.POST) if rform.is_valid(): print(rform.cleaned_data) username = rform.cleaned_data.get('username') email = rform.cleaned_data.get('email') mobile = rform.cleaned_data.get('mobile') password = rform.cleaned_data.get('password') # 数据库中注册 return HttpResponse('yiui')
def register(request): if request.method == 'POST': form = UserRegisterForm(request.POST) if form.is_valid(): user = form.save() login(request, user) return redirect('index') else: form = UserRegisterForm() return render(request, 'user/register.html', {'form': form})