def register(): form = UserRegisterForm() if request.method == 'GET': return render_template('register.html', form=form) if request.method == 'POST': # 校验传递的username,pw1,pw2是必填值,且长度要限制,账号是否存在判断 # 1. 定义form表单 if form.validate_on_submit(): # 账号密码全部校验成功 username = form.username.data password = form.pw1.data icon = form.icon.data # 保存账号信息 user = User() user.username = username user.password = generate_password_hash(password) if icon: # 保存图片,且保存图片字段 BASE_DIR = os.path.dirname( os.path.dirname(os.path.abspath(__file__))) STATIC_DIR = os.path.join(BASE_DIR, 'static') MEDIA_DIR = os.path.join(STATIC_DIR, 'media') # 保存图片的绝对路径 icon_path = os.path.join(MEDIA_DIR, icon.filename) icon.save(icon_path) # 保存图片的相对路径 user.icons = icon.filename db.session.add(user) db.session.commit() return render_template('register.html', form=form)
def user_register(): if current_user.is_authenticated: return redirect(url_for('index')) form = UserRegisterForm() if not form.validate_on_submit(): return render_template('register.html', form=form, testing=app.testing) else: username = form.username.data nickname = form.nickname.data password = form.password1.data email = form.email1.data print(username, nickname, password, email, flush=True) try: User.create(username=username, nickname=nickname, password=password, email=email) except IntegrityError as e: app.logger.warning(f"Integrity Error: {e}") crx_flash('ERROR_USER_EXISTS') return redirect(url_for('user_register')) except Exception as e: app.logger.warning(f"Unknown Error: {e}") crx_flash('ERROR_UNKNOWN') return redirect(url_for('user_register')) app.logger.info(f"User created: {username}, {nickname}, {email}") crx_flash('ACCOUNT_CREATED', username) return redirect(url_for('user_login'))
def users_register(): form = UserRegisterForm() mail = Mail(app) error = None twitter_name = request.args.get("tw") if request.method == "POST" and form.validate(): # now, validation done in forms.py if dbusers.user.find_one({"email": form.email.data}): error = "Email sudah terdaftar!" return render_template("/users/register.html", form=form, error=error) else: # simpan session username session["email"] = form.email.data # cek apakah dia daftar menggunakan twitter if twitter_name: dbusers.user.update({"twitter_name": form.twitter_name.data}, {"$set": { "firstname": form.firstname.data, "lastname": form.lastname.data, "password": bcrypt.generate_password_hash(form.password.data), "email": form.email.data, "joined": datetime.datetime.now(), "confirmed": True, # for email confirmation needs "completed": True, }}) else: # simpan data seperti biasa dbusers.user.insert({ "firstname": form.firstname.data, "lastname": form.lastname.data, "password": bcrypt.generate_password_hash(form.password.data), "email": form.email.data, "joined": datetime.datetime.now(), "confirmed": False, # for email confirmation needs "completed": True, }) # send email emailstring = """ Thank you for registering with example.com. Here is your credential info, please keep it secret: username: %s password: %s confirmation url: http://127.0.0.1:5000/users/activate/%s Regards, laporpakdhe.org """ % (form.email.data, form.password.data, get_activation_link(form.email.data)) msg = Message("Welcome to example.com", sender="*****@*****.**", recipients=[form.email.data]) msg.body = emailstring # mail.send(msg) # return redirect(request.args.get("next") or url_for("users")) return redirect(request.args.get("next") or url_for("users.users_index")) else: flash_errors(form) return render_template("/users/register.html", form=form, twitter_name=twitter_name)
def registerCheck(request): uf = UserRegisterForm(request.POST) if uf.is_valid(): uf.save() messages.success(request, 'Successfully Registered') return redirect('registerForm') else: messages.error(request, uf.errors) return render(request, 'registerForm.html', {'RegForm': uf})
def register_view(request): print(request.user.is_authenticated()) form = UserRegisterForm(request.POST, request.FILES or None) if form.is_valid(): user = form.save(commit=False) password = form.cleaned_data.get('password') im = form.cleaned_data.get('image') user.set_password(password) user.save() new_user = authenticate(username=user.username, password=password) login(request, new_user) return render(request, 'register_sucessfull.html', {}) context = { "form": form, } return render(request, 'register.html', context)
def register_view(request): next = request.GET.get('next') form = UserRegisterForm(request.POST or None) if form.is_valid(): user = form.save(request) password = form.cleaned_data.get('password') password = make_password(form.cleaned_data.get('password')) user.set_password(password) user.save() new_user = authenticate(username=user.username, password=password) login(request, new_user) if next: return redirect(next) return redirect('/') context = { 'form': form, } return render(request, "signup.html", context)
def register(): if current_user.is_authenticated: return redirect(url_for('posts_list')) form = UserRegisterForm() if request.method == 'POST' and form.validate(): user = User( username=form.username.data, email=form.email.data, first_name=form.first_name.data, second_name=form.second_name.data, ) user.set_password(form.password.data) db.session.add(user) db.session.commit() flash('Congrats. Account successfully created.') return redirect(url_for('login')) return render_template( 'register_form.html', form=form, )
def register(request): """ View for register returns register.html template """ if not request.user.is_authenticated(): context_dict={} if request.method == 'POST': user_form = UserRegisterForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save(commit=False) user.set_password(user.password) user.is_staff=True profile = profile_form.save(commit=False) user.save() profile.user = user profile.save() return HttpResponseRedirect(settings.REGISTER_REDIRECT_UTL) else: print user_form.errors print profile_form.errors context_dict["error1"] = user_form.errors context_dict["error2"] = user_form.errors else: user_form = UserRegisterForm() profile_form = UserProfileForm() context_dict["user_form"]=user_form context_dict["profile_form"]=profile_form return render(request, 'app/register.html',context_dict ) else : return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
def sign_up(request): context = { 'form': UserRegisterForm(), 'icon_message': 'success', 'title_message': 'Great' } if request.method == 'POST': data_form = UserRegisterForm(data=request.POST) if data_form.is_valid(): data_form.save() # loguear automaticamente con datos del formulario user = authenticate( username=data_form.cleaned_data['username'], password=data_form.cleaned_data['password1'] ) login(request, user) # redireccionar al home luego de un mensaje messages.success( request, "you have registered successfully!" ) return redirect(to='home') context['form'] = data_form return render(request, 'registration/sign_up.html', context)
def registrar_usuario(): form = UserRegisterForm() if form.validate_on_submit(): nombre = form.nombre.data email = form.email.data password = form.password.data nuevo_usuario = Usuario(nombre=nombre, email=email, password=password) db.session.add(nuevo_usuario) db.session.commit() app.logger.info(f'{datetime.now()}: User {nuevo_usuario} registered') flash('Estas registrado!', 'success') return redirect(url_for('home')) return render_template('registrar.html', barra_busqueda=True, titulo='Registrarse', form=form, debug=app.config['DEBUG'])
def register(): # 表单对象 form = UserRegisterForm() if request.method == 'GET': return render_template('register.html', form=form) if request.method == 'POST': # 验证提交的字段信息 if form.validate_on_submit(): 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('app.login')) else: # 验证失败, form.errors中存在错误信息 return render_template('register.html', form=form)
def post(self, request): register_form = UserRegisterForm(request.POST) if register_form.is_valid(): user = register_form.save() user.is_active = False user.set_password(request.POST['password1']) user.save() uid = urlsafe_base64_encode(force_bytes(user.pk)) activate_url = "{}/{}/{}".format( "http://localhost:8000/activate", uid, default_token_generator.make_token(user=user) ) send_email( recipient_list=[user.email], activate_url=activate_url ) return HttpResponse("Check your email list to activate account!") return HttpResponse("Wrong Data")
def register(request): if request.user.is_authenticated: return redirect('/') if request.method == 'POST': form = UserRegisterForm(request.POST) if form.is_valid(): form.save() username = form.cleaned_data.get('username') messages.success( request, f'Your account {username} has been created. Login now!') return redirect('login') else: form = UserRegisterForm() return render(request, 'register.html', {'form': form})
def get(self, request): register_form = UserRegisterForm() return render(request, 'registration.html', context={ 'form': register_form, })