def register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'profile_pic' in request.FILES: print('found it') profile.profile_pic = request.FILES['profile_pic'] profile.save() registered = True else: print(user_form.errors,profile_form.errors) else: user_form = UserForm() profile_form = UserProfileInfoForm() return render(request,'home/registration.html', {'user_form':user_form, 'profile_form':profile_form, 'registered':registered})
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 registration.html file page. return render( request, 'home/registration.html', { 'user_form': user_form, 'profile_form': profile_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): error_msg = "" if request.method == "POST": userform = UserForm(request.POST) # 将请求的数据加入表单进行校验 if userform.is_valid(): data = request.POST username = userform.cleaned_data.get("username") password = userform.cleaned_data.get("password") password_confirm = data.get("password_confirm") code = data.get("code") email = data.get("email") if password != password_confirm: error_msg = "两次密码不一致" return render(request, "common/register.html", {"errors": error_msg}) # 数据库保存用户注册信息 code1 = request.session.get("code") email1 = request.session.get("email") print(code1, email1, code, email) if code != code1 or email != email1: error_msg = "验证码与邮箱不符" return render(request, "common/register.html", {"errors": error_msg}) try: user = User() user.username = username user.password = setPassword(setPassword(password)) user.email = email user.save() return HttpResponseRedirect("/login/") except: error_msg = "邮箱或者用户名重复!" else: error_msg = userform.errors return render(request, "common/register.html", {"errors": error_msg})
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 registered(request): registered = False if request.method == "POST": print(request.POST) print(request.FILES) user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user profile.save() registered = True login(request, user) return redirect("/home/user-demand") else: # user_errors = user_form.errors # profile_errors = profile_form.errors print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileForm() return render( request, "registration/registration_form.html", { "user_form": user_form, "profile_form": profile_form, "registered": registered, }, )
def register(request): registered = True if request.method == "POST": userform = UserForm(data=request.POST) userInfo = UserProfileInfoForm(data=request.POST) if userform.is_valid() and userInfo.is_valid(): user_data = userform.save(commit=False) user_data.set_password(user_data.password) userprofile = userInfo.save(commit=False) user_data.save() userprofile.user = user_data userprofile.save() registered = False else: return render( request, 'signin.html', { 'registered': registered, 'userform': userform, 'userinfoform': userInfo }) userform = UserForm userinfoform = UserProfileInfoForm return render( request, 'signin.html', { 'registered': registered, 'userform': userform, 'userinfoform': userinfoform })
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 test_valid_form(self): mp = User.objects.create(title='Login Form', body='Bar') data = { 'title': mp.title, 'body': mp.body, } form = UserForm(data=data) self.assertTrue(form.is_valid())
def test_invalid_form(self): w = User.objects.create(title='Login Form', body='') data = { 'title': w.title, 'body': w.body, } form = UserForm(data=data) self.assertFalse(form.is_valid())
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 test_blank_data(self): form = UserForm({}) # form = UserForm({}, entry=self.entry) self.assertFalse(form.is_valid()) self.assertEqual( form.errors, { 'username': ['required'], 'email': ['required'], 'password': ['required'], })
def profile(request): if request.method == "POST": userform = UserForm(request.POST, instance=request.user) user_profile = UserProfile.objects.get(user=request.user) userprofileform = UserProfileForm(request.POST, instance=user_profile) try: pilot = Pilot.objects.get(user=request.user) pilotform = PilotProfileForm(request.POST, instance=pilot) pilot_valid = pilotform.is_valid() if pilot_valid: pilotform.save() except ObjectDoesNotExist: pilotform = None pilot_valid = True user_valid = userform.is_valid() user_profile_valid = userprofileform.is_valid() if pilot_valid and user_valid and user_profile_valid: change = "Changes Submitted" print("PILOT SAVED!") else: print("PILOT INVALID") if user_valid: userform.save() print("User SAVED!") if user_profile_valid: userprofileform.save() user_profile = UserProfile.objects.get(user=request.user) request.session["django_timezone"] = user_profile.timezone print("User Profile Saved!") else: print("USER INVALID") else: try: pilot = Pilot.objects.get(user=request.user) pilotform = PilotProfileForm(instance=pilot) except ObjectDoesNotExist: pilotform = None userform = UserForm(instance=request.user) userprofile = UserProfile.objects.get(user=request.user) userprofileform = UserProfileForm(instance=userprofile) change = "" return render( request, "home/profile.html", { "userform": userform, "change": change, "pilotform": pilotform, "userprofileform": userprofileform, }, )
def test_valid_data(self): form = UserForm({ 'username': "******", 'email': "*****@*****.**", 'password': "******", }) self.assertTrue(form.is_valid()) user = form.save() self.assertEqual(user.name, "Turanga Leela") self.assertEqual(user.email, "*****@*****.**") self.assertEqual(user.password, "Hi 2513698514258there")
def add_user(request): if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): form.save() username = form.cleaned_data.get('username') raw_password = form.cleaned_data.get('password1') user = authenticate(username=username, password=raw_password) login(request, user) return HttpResponseRedirect('/school') else: form = UserForm() return render(request, 'home/signup.html', {'form': form})
def register(request): print(request.POST) if request.method == 'POST': form = UserForm(request.POST) print(form) if form.is_valid(): form.save() return redirect('login') else: form = UserForm() return render(request, "register/register.html", {'form': form})
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 register(request): registered = False if request.method == 'GET': # display registration form user_form = UserForm() elif request.method == 'POST': user_form = UserForm(data=request.POST) if user_form.is_valid(): user = user_form.save() user.set_password(user_form.cleaned_data['password']) user.save() registered = True logger.info('User %s registered', user.username) else: logger.error(user_form.errors) return render(request, 'register.html', locals())
def signup_page(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) if user_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() registered = True else: print(user_form.errors) else: user_form = UserForm() return render(request, 'home/signup.html', {'user_form': user_form, 'registered': registered})
def signup(request): if request.method == 'POST': user_form = UserForm(request.POST) profile_form = ProfileForm(request.POST) print('40 = ', profile_form.errors.as_data()) print('41 = ', user_form.errors.as_data()) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() print('test') return render(request, 'home/home.html') else: user_form = UserForm() profile_form = ProfileForm context = {'user_form': user_form, 'profile_form': profile_form} template = 'home/signup.html' return render(request, template, context)
def update_profile(request): if request.method == "POST": user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, instance=request.user.profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() return HttpResponseRedirect('/') else: messages.error(request, _('Please correct the error below.')) else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) return render(request, 'home/profile.html', { 'user_form': user_form, 'profile_form': profile_form })
def register(request): errors = "" if request.method == "POST": userform = UserForm(request.POST) # 将请求的数据加入表单进行校验 if userform.is_valid(): username = userform.cleaned_data.get("username") # 校验过的数据 password = userform.cleaned_data.get("password") password_confirm = request.POST.get("password_confirm") if password == password_confirm: # 数据库保存用户注册信息 user = User() user.username = username user.password = setPassword(setPassword(password)) user.save() return HttpResponseRedirect("/login/") # 如果注册成功,跳转到登陆 else: errors = userform.errors return render(request, "common/register.html", {"errors": errors})
def register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) if user_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() return render(request, 'home/login.html', {'user_form': user_form}) else: print(user_form.errors) return render(request, 'home/registration.html', {'user_form': user_form}) else: user_form = UserForm() return render(request, 'home/registration.html', {'user_form': user_form})
def register(request): registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user profile.save() registered = True else: print(user_form.errors, profile_form.errors) return HttpResponse("Registered")
def update_profile(request): if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, instance=request.user.profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() messages.success(request, 'Your profile was succesfully updated!') return redirect('/profile/') else: messages.error(request, 'Please correct the error bellow') else: user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) context = { 'user_form': user_form, 'profile_form': profile_form } return render(request, 'home/update_profile.html', context)
def test_blank_data(self): form = UserForm({}) # form = UserForm({}, entry=self.entry) self.assertFalse(form.is_valid()) self.assertEqual( form.errors, { 'FirstName': ['required'], 'SecondNam': ['required'], 'AgeBeforeMissing': ['required'], 'DateOfBirth': ['required'], 'HairColour': ['required'], 'EyesColour': ['required'], 'Weigh': ['required'], 'Height': ['required'], 'MissingFrom': ['required'], 'MissingDate': ['required'], 'RelativeID': ['required'], 'RelativeRelation': ['required'], 'Details': ['required'], 'MissingPersonImage': ['required'], })
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 register(request): registered = False if request.method == "POST": user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'profile_pic' in request.FILES: profile.profile_pic = request.FILES['profile_pic'] profile.save() registered = True return render( request, 'home/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered }) else: my_dict = {'register_alert': "User Already Registered"} return render(request, 'home/login.html', context=my_dict) else: user_form = UserForm() profile_form = UserProfileInfoForm() return render( request, 'home/registration.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
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) # If the forms are valid... if user_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() # 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 # 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() # Render the template depending on the context. return render(request, 'auth/register.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 admin(request): registered = False admin_auth_key = "jrk898989#" error = False error_value = "" if request.method == 'POST': user_form = UserForm(data=request.POST) inp_admin_ac = request.POST.get('id_admin_auth') inp_confirm_pass = request.POST.get('id_password_confirm') if(inp_admin_ac == admin_auth_key): if(inp_confirm_pass == request.POST.get('password')): if user_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() registered = True else: error=True error_value = user_form.errors print(error_value) else: error=True error_value = "Password are not matching" print(error_value) else: error=True error_value = "Invalid Auth Code" print(error_value) return render(request,'home/admin.html',{'userform':user_form,'registered':registered,'error':error,'error_value':error_value}) else: userform = UserForm() return render(request,'home/admin.html',{'userform':userform,'registered':registered,'error':error,'error_value':error_value})
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)