def inscription(request): if request.method == "POST": form = UserForm(request.POST) if form.is_valid(): # On sauvegarde le nouveau membre form.save() # On envoie un mail pour dire qu'il a bien envoyé sa requête d'inscription ! sujet = _("Inscription à Eliteheberg") mail = _( "Bonjour. Vous avez bien demandé un compte d'hébergement sur Eliteheberg. Nous vous contacterons bientôt pour vous indiquer votre espace d'hébergement. Merci." ) try: send_mail(mail, "*****@*****.**", form.email) txtmessage = _("Votre inscription a bien été enregistrée.") messages.add_message(request, messages.SUCCESS, txtmessage) except BadHeaderError: return HttpResponse(status=500) # On le redirige vers l'accueil (pour l'instant une erreur 404) return redirect("home.views.home", {"form": UserForm()}) else: form = UserForm() return render(request, "home/inscription.html", {"form": form})
def sign_in(request): # Set to False initially. registered changes value to True when registration succeeds. registered = False # Se for um POST estamos a processar dados que vêm do REQUEST. if request.method == 'POST': user_form = UserForm(data=request.POST) # Se o formulário vem bem preenchido... if user_form.is_valid(): user = user_form.save() # set_password encripta a password enviada para colocar na BD. user.set_password(user.password) user.save() # Agora sim, o registo ficou feito e gravado. registered = True else: # They'll also be shown to the user. print user_form.errors #Nao sendo um post, estão a pedir o Formulário para preenchimento de um novo user else: user_form = UserForm() # Render the template depending on the context. return render(request, 'sign_in.html', {'user_form': user_form, 'registered': registered} )
def signup_view(request): password1 = "" password2 = "" nombre = "" apellidos = "" username = "" email1 = "" if request.method == "POST": formulario = UserForm(request.POST) if formulario.is_valid(): nombre = formulario.cleaned_data['nombre'] apellidos = formulario.cleaned_data['apellidos'] username = formulario.cleaned_data['username'] email1 = formulario.cleaned_data['email1'] password1 = formulario.cleaned_data['password1'] b = User() b.insertar (nombre,apellidos,username,email1,password1) b.save() #configuracion enviando mensaje via GMAIL user= '******'%(email1); html_content = "<p>Bienvenido a LinkJobs</p> <p>Tus datos personales son los siguientes:</p><p><b>Nombre:</b> %s</p><p><b>Apellidos:</b> %s</p><p><b>Username:</b> %s</p><p><b>email:</b> %s</p><p><b>Password:</b> %s</p>"%(nombre,apellidos,username,email1,password1) msg= EmailMultiAlternatives('Registro exitoso en LinkJobs',html_content,'*****@*****.**',[user]) # SUBJECT,CONTENT,DESTINATARIO msg.attach_alternative(html_content,'text/html') #definimos el contenido como html msg.send() #enviamos #fin configuracion del servidor GMAIL return HttpResponseRedirect(reverse('home.views.login_view')) else: formulario = UserForm() ctx = {'formulario':formulario} return render_to_response ('home/signup.html',ctx,context_instance=RequestContext(request))
def register(request): # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user # Did the user provide a profile picture? # If so, we need to get it from the input form and put it in the UserProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print (user_form.errors, profile_form.errors) # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render(request, 'home/registration/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} )
def register(request): if request.method == 'GET': user_form = UserForm() return render(request, 'home/register.html', {'user_form': user_form}) else: post = request.POST user_form = UserForm(post) if user_form.is_valid(): if post['password_one'] == post['password_two']: user = User.objects.create_user(post['username'], post['email'], post['password_one']) return HttpResponseRedirect('/user/') else: errors = {'error': 'passwords do not match'} return render(request, 'home/register.html', {'user_form': user_form, 'errors': errors}) else: return render(request, 'home/register.html', {'user_form': user_form})
def sign_up(request): # Set to False initially. registered changes value to True when # registration succeeds. registered = False # Se for um POST estamos a processar dados que vem do REQUEST. if request.method == 'POST': user_form = UserForm(data=request.POST) # Se o formulario vem bem preenchido... if user_form.is_valid(): user = user_form.save() # set_password encripta a password enviada para colocar na BD. user.set_password(user.password) user.save() # adicionar o utilizador ao grupo "site_user" site_user_group = Group.objects.get(name='site_users') site_user_group.user_set.add(user) # verificar se ficou OK, i.e. se o utilizador pertence ao grupo if user.groups.filter(name="site_users").count(): print("user added to group") # Agora sim, o registo ficou feito e gravado. registered = True else: # Os erros sao passados ao user tb, mas esta parte imprime na linha de comando. print user_form.errors #Nao sendo um post, estao a pedir o Formulario para preenchimento de um novo user else: user_form = UserForm() # Render the template depending on the context. return render(request, 'sign_up.html', {'user_form': user_form, 'registered': registered} )
def view_profile(request): user = request.user user_form = UserForm(instance=user) ProfileInlineFormset = inlineformset_factory(User, UserProfile, can_delete=False, fields=('description', 'address', 'education', 'phone', 'image')) formset = ProfileInlineFormset(instance=user) if request.user.is_authenticated and request.user.id == user.id: if request.method == "POST": user_form = UserForm(request.POST, request.FILES, instance=user) formset = ProfileInlineFormset(request.POST, request.FILES, instance=user) if user_form.is_valid(): created_user = user_form.save(commit=False) formset = ProfileInlineFormset(request.POST, request.FILES, instance=created_user) if formset.is_valid(): created_user.save() formset.save() return redirect('view_profile') try: progress = Progress.objects.get(user=request.user) achievements = json.loads(progress.all_quiz_records) achievements = achievements.get('all_records') except Progress.DoesNotExist: achievements = None return render(request, "home/profile.html", { 'user': user, "noodle_form": user_form, "formset": formset, 'achievements':achievements, }) else: raise PermissionDenied
def register(request): # Like before, get the request's context. context = RequestContext(request) # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database. user = user_form.save() # Now we hash the password with the set_password method. # Once hashed, we can update the user object. user.set_password(user.password) # Did the user provide a group? # If so, we need to get it from the post and put it in the User model, empty otherwise. user.groups.add(request.POST.get('group', '')) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user # Did the user provide a profile picture? # If so, we need to get it from the input form and put it in the UserProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance. profile.save() # Update our variable to tell the template registration was successful. registered = True # Register this user to all candidate questions registerNewUserToAllCandidateQuestions(user) # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print user_form.errors, profile_form.errors # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render_to_response( 'home/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered }, context)
def user(): """ 用户信息中心视图 :return: user.html """ form = UserForm() user = User.query.get(int(session['user_id'])) form.face.validators = [] if request.method == "GET": form.name.data = user.name form.email.data = user.email form.phone.data = user.phone form.card.data = user.card form.info.data = user.info form.address.data = user.address form.location.data = user.location if form.validate_on_submit(): data = form.data if form.face.data != "": file_face = secure_filename(form.face.data.filename) if not os.path.exists(config.FACE_FOLDER): os.makedirs(config.FACE_FOLDER) os.chmod(config.FACE_FOLDER) user.face = change_name(file_face) form.face.data.save(config.FACE_FOLDER + user.face) name_count = User.query.filter_by(name=data['name']).count() if data['name'] != user.name and name_count == 1: flash("用户名已经存在", 'err') return redirect(url_for('home.user')) email_count = User.query.filter_by(email=data['email']).count() if data['email'] != user.email and email_count == 1: flash("邮箱已经存在", 'err') return redirect(url_for('home.user')) phone_count = User.query.filter_by(phone=data['phone']).count() if data['phone'] != user.phone and phone_count == 1: flash("手机号码已经存在", 'err') return redirect(url_for('home.user')) card_count = User.query.filter_by(card=data['card']).count() if data['card'] != user.card and card_count == 1: flash("银行卡号码已经存在", 'err') return redirect(url_for('home.user')) user.name = data['name'] user.email = data['email'] user.phone = data['phone'] user.card = data['card'] user.info = data['info'] user.address = data['address'] user.location = data['location'] db.session.add(user) try: db.session.commit() except: db.session.rollback() flash("你的信息已经修改成功", 'ok') return redirect(url_for('home.user')) return render_template("home/user.html", form=form, user=user)
def register(request): registered = False if request.method == 'POST': # Get info from "both" forms # It appears as one form to the user on the .html page user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) # Check to see both forms are valid if user_form.is_valid() and profile_form.is_valid(): # Save User Form to Database user = user_form.save() # Hash the password user.set_password(user.password) # Update with Hashed password user.save() # Now we deal with the extra info! # Can't commit yet because we still need to manipulate profile = profile_form.save(commit=False) # Set One to One relationship between # UserForm and UserProfileInfoForm profile.user = user # Check if they provided a profile picture if 'profile_pic' in request.FILES: print('found it') # If yes, then grab it from the POST form reply profile.profile_pic = request.FILES['profile_pic'] # Now save model profile.save() # Registration Successful! registered = True else: # One of the forms was invalid if this else gets called. print(user_form.errors, profile_form.errors) else: # Was not an HTTP post so we just render the forms as blank. user_form = UserForm() profile_form = UserProfileInfoForm() # This is the render and context dictionary to feed # back to the body.html file page. return render( request, 'home/index.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })