def register(request): # get context as always context = RequestContext(request) cat_lsit = get_category_list() # boolean to determine if registraton was succesful registered = False # if its HTTP POST then processing form data if request.method == 'POST': # try and grab info from raw form user_form = UserForm(data = request.POST) profile_form = UserProfileForm(data = request.POST) # if forms are valid if user_form.is_valid() and profile_form.is_valid(): # save the form to database user = user_form.save() # hash the password to set password method user.set_password(user.password) user.save() # sort user profile instance, set as false to avoid integrity problems profile = profile_form.save(commit = False) profile.user = user # update registration boolean now registration is finished registered = True #invalid form so print error else: print( user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileForm() # reder template context_dict = {'user_form': user_form, 'profile_form': profile_form, 'registered': registered, 'cat_list': cat_list} return render_to_response('rango/register.html', context_dict, context)
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 faw 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 instane profile.save() # Update our variable to indicate that the template # registartion was successful registered = True else: # Invalid form or forms - mistakes or something else # Print problems to the terminal. print(user_form.errors, profile_form.errors) else: # Not a HTTP POST, so we render our form using two ModelForm instances # These forms will be blank, ready for user input user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context return render(request,'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
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 if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors(), profile_form.errors() else: user_form = UserForm() profile_form = UserProfileForm() return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
def register(request): context = RequestContext(request) registered = False # Post request 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() # does this really save form data to the database? user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors, profile_form.errors # not a HTTP POST else: user_form = UserForm() profile_form = UserProfileForm() # render template return render_to_response('rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered, 'cat_list': get_category_list()}, context)
def register(request): #if request.session.test_cookie_worked(): # print ">>> TEST COOKIE WORKED..." # request.session.delete_test_cookie() context = RequestContext(request) registered = False if request.method =='POST': user_form = UserForm(data=request.POST) #gets the information entered into the form 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 if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render_to_response( 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)
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(): # save user's form data to the db user = user_form.save() # Hash the PASSWORD user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user # Did the user provide a profile picture? if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print(user_form.errors, profile_form.errors) # not POST HTTP else: user_form = UserForm() profile_form = UserProfileForm() return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
def registration_register(request): # if request.session.test_cookie_worked(): # print ">>>>> TEST COOKIE WORKED!" # request.session.delete_test_cookie() 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 if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
def register(request): context=RequestContext(request) registered = False if(request.method=="POST"): #process the form 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()#hashing the pwd profile=profile_form.save(commit=False) profile.user=user#This is where we populate the user attribute of the UserProfileForm form, which we hid from users if 'picture' in request.FILES: profile.picture=request.FILES['picture'] profile.save() registered=True else: print user_form.errors, profile_form.errors else: user_form=UserForm() profile_form=UserProfileForm() return render_to_response( 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)
def register(request): registered = False if request.method == 'POST': 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(): user = user_form.save() user.set_password(user.password) # Hash the password user.save() # Save the password profile = profile_form.save(commit=False) profile.user = user # Check if user provides a profile picture if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors, profile_form.errors # If this is not a HTTP POST, then render the forms for input else: user_form = UserForm() profile_form = UserProfileForm() return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
def register(request): context = RequestContext(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 if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: user_form = UserForm() profile_form = UserProfileForm() return render_to_response('rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered,}, context)
def register(request): #Like before, get the request's context context = RequestContext(request) cat_list = get_category_list() # A boolean value for telling the template whether the registration was successful # Set to False initiall. 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 somethign 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 to ModelForm instances # These forms will be blank, ready for user input else: user_form = UserForm() profile_form = UserProfileForm() context_dict = {'user_form': user_form, 'profile_form': profile_form, 'registered': registered, 'cat_list': cat_list} # Render the template depending on the context return render_to_response('rango/register.html', context_dict, context)
def register(request): # boolean for telling the template whether registration is successful if request.session.test_cookie_worked(): print ">>>> TEST COOKIE WORKED!" request.session.delete_test_cookie() register = 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 if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() register = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'register': register})
def register(request): context = RequestContext(request) registered = False if request.session.test_cookie_worked(): print ">>>>>TEST COOKIE WORKED" request.session.delete_test_cookie() if request.POST: form = UserForm(request.POST) profile_form = UserProfileForm(request.POST) if form.is_valid() and profile_form.is_valid(): user = form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user profile.picture = request.FILES['picture'] profile.save() registered = True else: form.errors, profile_form.errors else: form = UserForm() profile_form = UserProfileForm() return render_to_response('rango/register.html',{'form':form,'profile_form':profile_form,'registered':registered},context)
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 if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form=UserForm() profile_form = UserProfileForm() context_dict = {'user_form': user_form, 'profile_form':profile_form, 'registered':registered} bar_list = Bar.objects.order_by('-numero_visitas') context_dict['bares'] = bar_list return render(request, 'rango/register.html', context_dict)
def register(request): if request.session.test_cookie_worked(): print ">>>>Test cookie worked !!!" request.session.delete_test_cookie() 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() print "Part 1 done" profile = profile_form.save(commit = False) profile.user = user print "Part 1.5 done" if 'pic' in request.FILES: profile.pic = request.FILES['pic'] profile.save() print "Part 2 done" registered = True else: print user_form.errors, profile_form.errors else: print "Part 3 executing" user_form = UserForm() profile_form = UserProfileForm() print "registered = ",registered return render(request, 'rango/register.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 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 if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileForm() return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
def register(request): if request.session.test_cookie_worked(): print 'Test cookie worked' request.session.delete_test_cookie() context = RequestContext(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 if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() context_dict = {'user_form' : user_form, 'profile_form': profile_form, 'registered':registered} return render_to_response('rango/register.html', context_dict, context)
def register(request): # Value if the registration was successful. Changes in the end of the process 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() # Use commit to delay to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now saving profile. profile.save() registered = True else: print user_form.errors, profile_form.errors # Render the template depending on the context return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} )
def register(request): registered = False ## Testing cookies if request.session.test_cookie_worked(): print ">>>>> Test cookie worked!" # request.session.delete_test_cookie() ## Testing cookies 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 if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render(request, 'rango/register.html', {'user_form':user_form, 'profile_form':profile_form, 'registered': registered, 'cat_list':cat_list, })
def register(request): registered = False context = {} template = loader.get_template('rango/register.html') 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.save() profile = profile_form.save(commit=False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES('picture') profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() context['user_form'] = user_form context['profile_form'] = profile_form context['registered'] = registered return HttpResponse(template.render(context, request))
def register(request): #if request.session.test_cookie_worked(): # print ">>>> TEST COOKIE WORKED!" # request.session.delete_test_cookie() 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 if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True #user1 = authenticate(username = user.username,password=user.password) #login(request,user1) #return HttpResponseRedirect('/rango/') else : print user_form.errors, profile_form.errors else : user_form = UserForm() profile_form = UserProfileForm() return render(request,'rango/register.html', {'user_form':user_form,'profile_form':profile_form,'registered':registered})
def register(request): registed = False if request.method == "POST": user_form = UserForm(request.POST) user_profile_form = UserProfileForm(request.POST) if user_form.is_valid() and user_profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() user_profile = user_profile_form.save(commit = False) user_profile.user = user if 'picture' in request.FILES: print "co picture" user_profile.picture = request.FILES['picture'] user_profile.save() registed = True else: user_form = UserForm() user_profile_form = UserProfileForm() return render(request,'rango/register.html',{ 'user_form' : user_form, 'user_profile_form' : user_profile_form, 'registed' : registed, })
def register(request): registered = False print 'reg', request.method 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 if 'picture' in request.FILES: profile.picture = request.FILES('picture') profile.save() registered = True else: print 'user profile form errors', user_form.errors, profile_form.errors else: user_form=UserForm() profile_form = UserProfileForm() context = {'user_form': user_form, 'profile_form': profile_form,'registered': registered} print 'context', context return render(request, 'rango/register.html', context)
def register(request): context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) calc_form = CalcForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid() and calc_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() calc = calc_form.save(commit=False) calc.user = user calc.gender = profile.gender calc.height_for_calc = profile.height calc.weight_for_calc = profile.weight calc.save() registered = True else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileForm() calc_form = CalcForm() return render_to_response('rango/register.html',{'user_form': user_form, 'profile_form': profile_form, 'calc_form': calc_form, 'registered' : registered}, context)
def register(request): context = RequestContext(request) registered = False #Set to False initially. Code changes value to True when registration succeeds. if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfilForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) # Hash the password with the set_password method. user.save() profile = profile_form.save(commit=False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfilForm() return render_to_response( 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)
def register(request): if request.session.test_cookie_worked(): print ">>>Test cookie worked!" request.session.delete_test_cookie() context = RequestContext(request) categories = get_category_list() 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 if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render_to_response('rango/register.html', dict(user_form=user_form, profile_form=profile_form, registered=registered, categories=categories), context)
def register(request): if request.session.test_cookie_worked(): print ">>>> TEST COOKIE WORKED!" request.session.delete_test_cookie() # 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, "rango/register.html", {"user_form": user_form, "profile_form": profile_form, "registered": registered} )
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 if "picture" in request.FILES: profile.picture = request.FILES["picture"] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render( request, "rango/register.html", {"user_form": user_form, "profile_form": profile_form, "registered": registered} )
def register(request): context = RequestContext(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 if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors , profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm()
def register(request): context = RequestContext(request) context_dict = {'cat_list': get_category_list()} 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 if 'picture' in request.FILES: profile.picure = request.FILES['picture'] profile.save() registered = True else: context_dict['errors'] = user_form.errors + profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() context_dict['user_form'] = user_form context_dict['profile_form'] = profile_form context_dict['registered'] = registered return render_to_response('rango/register.html', context_dict, context)
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, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): #a boolean value for telling the template # whether the registration is successful # initially false, when succeeds true. registered = False # POST, handle the form data if request.method == "POST": # try to grab information from the raw form information # note that, we use both UserForm & UserProFileForm(additional attributes) user_form = UserForm(request.POST) profile_form = UserProfileForm(request.POST) # check whether the form data is valid if user_form.is_valid() and profile_form.is_valid(): # save the UserForm data into database user = user_form.save() # Note that using the hasher to set password # And update the user instance user.set_password(user.password) user.save() # handle the UserProfile instance # we need to set the user attribute ourselves(self-defined) #we set commit = False, to <delay> the save(iniitially no user instance) profile = profile_form.save(commit=False) profile.user = user # if user provide the icon # get it from the input form then put it into UserProfile mdoel if 'picture' in request.FILES: profile.picture = request.FILES['picture'] #save the UserProfile(Form to database) model instance profile.save() # tells the teplates, registration finished registered = True else: # invalid forms data # print problems to the terminal print(user_form.errors, profile_form.errors) else: # not HTTP POST(maybe get HttpRequest) # so provide&tender the blank form for user user_form = UserForm() profile_form = UserProfileForm() # Render the template return render(request, 'rango/register.html', context={ '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 false initially. 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 attributes ourselves, we set commit=False #This delays saving the model until we're ready to avoid integrity problems profile = profile_form.save(commit=False) #We need to establish a link between the two model instances tht we have #created. After creaing a new User model instance, we reference it in #the UserProfile instance with the line below. This is where we #populate the user attribute of the UserProfileForm form, which we #hid this field from users profile.user = user #Did the user provide a pro pic? #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 indicate that the template registration was successful registered = True else: #Invalid form or forms - mistakes or something else? #Print problems to the terminal print(user_form.errors, profile_form.errors) else: #Not a HTTP POST, so we render our form using two ModelForm instances. #These forms will be blank, ready for user input. user_form = UserForm() profile_form = UserProfileForm() return render( request, 'rango/register.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, use code change the value to True when registration succeeds registered = False # If it is a HTTP POST, we r interested in processing from data if request.method == 'POST': # grab information form the raw form information # Note that we use both UserForm and UserProfileForm user_form = UserForm(request.POST) profile_form = UserProfileForm(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 oursleves # we set commit=False which delays saving the model profile = profile_form.save(commit=False) profile.user = user # check whether the user provided a profile picture # If so, get it from the input form and put it in UserProfile Model if 'picture' in request.FILES: profile.picture = request.FILES('picture') # Now we can save profile.save() # Update our variable to indicate that the template registration was successful registered = True else: print(user_form.errors, profile_form.errors) else: # Not a HTTP POST, so we render our form using ModelForm instances # These forms will be blank, ready for user input user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context return render(request, 'rango/register.html', context={ 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): # Request the context. context = RequestContext(request) cat_list = get_category_list() context_dict = {} context_dict['cat_list'] = cat_list # Boolean telling us whether registration was successful or not. # Initially False; presume it was a failure until proven otherwise! registered = False # If HTTP POST, we wish to process form data and create an account. if request.method == 'POST': # Grab raw form data - making use of both FormModels. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # Two valid forms? if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data. That one is easy. user = user_form.save() # Now a user account exists, we hash the password with the set_password() method. # Then we can update the account with .save(). user.set_password(user.password) user.save() # Now we can sort out the UserProfile instance. # We'll be setting values for the instance ourselves, so commit=False prevents Django from saving the instance automatically. profile = profile_form.save(commit=False) profile.user = user # Profile picture supplied? If so, we put it in the new UserProfile. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the model instance! profile.save() # We can say registration was successful. registered = True # Invalid form(s) - just print errors to the terminal. else: print user_form.errors, profile_form.errors # Not a HTTP POST, so we render the two ModelForms to allow a user to input their data. else: user_form = UserForm() profile_form = UserProfileForm() context_dict['user_form'] = user_form context_dict['profile_form'] = profile_form context_dict['registered'] = registered # Render and return! return render_to_response('rango/register.html', context_dict, context)
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 its a HTTP POST, we're interested in processing form data if request.method == 'POST': 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() # Sort out the user profile instance # Since we need to set the user attribute outselves, we set commit = False. # This delays ssaving the model until we're ready to avoid integrity problems profile = profile_form.save(commit=False) profile.user = user # Did user provide a profile picture? If so, 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 indicate that the template registration was successful registered = True else: # invalid form or forms = mistakes or something else? # print problems to the terminal print(user_form.errors, profile_form.errors) else: # Not a http post, so we render our form using 2 ModelForm instances # These forms will be blank, ready for user input user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context return render( request, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): # A boolean value for denoting whether or not registration was successful. registered = False # If the method is POST we are interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form data. # Note that we make use of both the UserForm and UserProfileForm user_form = UserForm(request.POST) profile_form = UserProfileForm(request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the users 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 we 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. 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 can save the profile instance. profile.save() # Update the variable to indicate that registration was successful. registered = True else: print(user_form.errors, profile_form.errors) else: # Not a HTTP POST, we render our view using two ModelForm instances. user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render(request, 'rango/register.html', context={ 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): # Логическое значение указывающее шаблону прошла ли регистрация успешно. # В начале ему присвоено значение False. Код изменяет значение на True, если регистрация прошла успешно. registered = False # Если это HTTP POST, мы заинтересованы в обработке данных формы. if request.method == 'POST': # Попытка извлечь необработанную информацию из формы. # Заметьте, что мы используем UserForm и UserProfileForm. 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() # Теперь мы хэшируем пароль с помощью метода set_password. # После хэширования мы можем обновить объект "пользователь". user.set_password(user.password) user.save() # Теперь разберемся с экземпляром UserProfile. # Поскольку мы должны сами назначить атрибут пользователя, необходимо приравнять commit=False. # Это отложит сохранение модели, чтобы избежать проблем целостности. profile = profile_form.save(commit=False) profile.user = user # Предоставил ли пользователь изображение для профиля? # Если да, необходимо извлечь его из формы и поместить в модель UserProfile. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Теперь мы сохраним экземпляр модели UserProfile. profile.save() # Обновляем нашу переменную, чтобы указать, что регистрация прошла успешно. registered = True # Неправильная формы или формы - ошибки или ещё какая-нибудь проблема? # Вывести проблемы в терминал. # Они будут также показаны пользователю. else: print(user_form.errors, profile_form.errors) # Не HTTP POST запрос, следователь мы выводим нашу форму, используя два экземпляра ModelForm. # Эти формы будут не заполненными и готовы к вводу данных от пользователя. else: user_form = UserForm() profile_form = UserProfileForm() # Выводим шаблон в зависимости от контекста. return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} )
def register(request): #boolean to tell template true if registration succeeds. registered = False #if it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': #attempt to grab info from the raw form information. #need make user 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 user's form data to the database. user = user_form.save() #hash password for security, then update the user object. user.set_password(user.password) user.save() #to sort out Userprofile instance. #since we need to set the user attributes 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 #user provided a dp? #true, get it from input form and put it in the userprofile model if 'picture' in request.FILES: profile.picture = request.FILES['picture'] #now save the userprofile model instance. profile.save() #update our variable to indicate that the template #registration was successful. registered = True else: #invalid form ot forms - mistakes or soemthing else? #print problems to the terminal. print(user_form.errors, profile_form.errors) else: #not a HTTP POSt, so we render our form using two modelform instances. #these forms will be blank, ready for user input. user_form = UserForm() profile_form = UserProfileForm() #ender the template depending on the context. return render( request, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): #A boolean value telling the template whether #the registration was successfule #set false init. code change to true when register #success registered = False user_form = UserForm() profile_form = UserProfileForm() #if it is a HTTP Post, we process the data if request.method == 'POST': #grap information from the raw form info #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 form data to the db user = user_form.save() #hash the password with the set_password method #after hashed update the user object user.set_password(user.password) user.save() #sort the UserProfile instance #need to set the user attribute ourselves # set commit = False. This delays saving the model until #we ready to avoid integrity problems profile = profile_form.save(commit=False) profile.user = user #Did the user provide a profile picture? # If so, get it in the input form and put it in the # UserProfile model if 'picture' in request.FILES: profile.picture = request.FILES['picture'] #Save the UserProfile mode instance profile.save() #Update our variable to indicate the the template registration #was successful registered = True else: print(user_form.errors, profile_form.errors) context_dict = { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered } return render(request, 'rango/register.html', context_dict)
def register(request): # like before, get request context context = RequestContext(request) # a boolean value for telling template whether registration was successful # set false initially. code changes value to true when registration succeed registered = False # if it's a HTTP POST, we're interested in processing form data if request.method == 'POST': # attempt to grab information from raw form information # note we make use of both UserForm and UserProfileForm user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # if two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # save user's form data to the database user = user_form.save() # now we hash the password with 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 user attribute ourselvs we set commit=False # this delays saving model until 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 input form, put it in UserProfile model if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # now we save the UserProfile instance profile.save() # update our variable to tell template registration successful registered = True # invalid form or forms - mistakes or something else? # print problems to terminal # they'll be shown to user else: print user_form.errors, profile_form.errors # not a HTTP POST so we render our form using two ModelForm instances # these forms be blank, ready for user input else: user_form = UserForm() profile_form = UserProfileForm() # render the template depending on the context return render_to_response( 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered }, context)
def register(request): #Bool for login (Changes to true when logged in) 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(): user = user_form.save() #Hash and save password 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 #If we have a profile picture, deal with it if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() #Update 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, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): # 用来返回给模版文件注册是否成功的标识变量。 # 初始设置为 False, 注册成功后设置为 True registed = False # 如果请求的类型是 POST, 那么我们将对提交的注册信息进行处理 if request.method == 'POST': # 试图从原始提交的表格信息中提取有用的信息 # 需要注意的是我们同时需要 UserForm 和 UserProfileForm的信息 user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # 假如两个 Form的数据都通过校验... if user_form.is_valid() and profile_form.is_valid(): # 将用户注册 Form 中的信息直接存入数据库 user = user_form.save() # 使用 set_password方法,来对用户的密码进行哈希算法的加密 # 加密完成后,应该更新用户的密码数据,以在数据库中保存 user.set_password(user.password) user.save() # 现在开始处理用户档案数据的Form信息,因为我们还需要补充输入相关的属性数据 # 所以我们在这里设置 commit=False ,来延迟数据被直接存入数据库 # 当我们完成所有的设置工作后,再真正提交数据库保存,这样能保证数据的完整性,避免异常的BUG profile = profile_form.save(commit=False) profile.user = user # 在这里我们判断用户是否上传了头像图片 # 如果用户上传了,那么我们需要从提交Form的数据中取出文件,并更新到我们的用户档案model中去 if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # 现在我们对用户档案的模型进行保存,即存入了数据库 profile.save() # 更新标识变量,以告诉模板我们已经注册成功了 registed = True else: # 如果 Form 数据校验不通过——存在某些错误 # 在 Terminal 中将错误打印出来 print(user_form.errors, profile_form.errors) else: # 如果请求类型不是 POST,那么我们将用使用两个 ModelForm 的实例来进行模板渲染 # 并且这两个实例的数据为空 user_form = UserForm() profile_form = UserProfileForm() # 返回模板渲染,并传入上下文参数 return render(request, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registed': registed })
def register(request): #boolean was registration successful? registered = False #if http post, we want to process form data if request.method == 'POST': #attempt to grab information from the raw form info #note that we make user of both UserForm and UserProfileForm user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) #if two forms are valid if user_form.is_valid() and profile_form.is_valid(): #save users form to db user = user_form.save() #now we hash password with set_password method #once hashed, we can update the user object user.set_password(user.password) user.save() #now sort out UserProfile instance #since we need to set the user attribute ourselves, we set #commit=False. this delays saving 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 yes, get it from input form and put in userprofile model if 'picture' in request.FILES: profile.picture = request.FILES['picture'] #now save UserProfile model instance profile.save() #update our variable to indicate that the template registration #was successful registered = True else: #invalid form or forms, print problems to terminal print(user_form.errors, profile_form.errors) else: #not http post, so we render our form using two ModelForm instances #these forms will be blank, ready for user input user_form = UserForm() profile_form = UserProfileForm() #render the template depending on the context return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
def register(request): # boolean to tel the template whether registration was successful. # set to false to start, code changes to true when successful registered = False # if it's HTTP POST, we want to proccess form data if request.method == 'POST': # attempt to grab info from raw form info # use both UserForm and UserProfileForm user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # if both forms are valid if user_form.is_valid() and profile_form.is_valid(): # save form data to database user = user_form.save() # now hash the password with the set_password method # once hashed, update user object user.set_password(user.password) user.save() # now sort out the UserProfile instance. Since we set user # attributes 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 user provide profile pic? If so, get it from input form # and put it in the UserProfile models if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # now save UserProfile model instance profile.save() # update variables to indicate template reg. was successful registered = True else: # invalid form/s - mistake or something else? # print problem to the terminal for user print(user_form.errors, profile_form.errors) else: # not HTTP POST, so render the form using two ModelForm instances. # These forms will be blank, ready for user input. user_form = UserForm() profile_form = UserProfileForm() return render( request, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): # A boolean value for telling the template whether registration was successful # Set to False initially, changed to True when registration succeeds registered = False # If it's a HTTP POST we process form data if request.method == 'POST': # Attempt to grab info user_form = UserForm(request.POST) profile_form = UserProfileForm(request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save user's form data to 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 profile = profile_form.save(commit=False) profile.user = user # Check if the user provided a picture # If so, we take 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 instance profile.save() # Update our variable to indicate that the registration was successful registered = True else: # Invalid form - mistakes made in either form print(user_form.errors, profile_form.errors) else: # Not a HTTP POST, so we render our form using two ModelForm instances # These forms will be blank, ready for input user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on context return render(request, 'rango/register.html', context={ 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): # Boolean telling whether the registration was successful registered = False 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(request.POST) profile_form = UserProfileForm(request.POST) # If both of the forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the form data to the database user = user_form.save() # set_password method is used for hashing # once hashed, update the 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 form the input form and # put it into the UserProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: # Invalid form or forms, print problems to the terminal print(user_form.errors, profile_form.errors) else: # Not a HTTP POST, so we render our form using two ModelForm instances. user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render(request, 'rango/register.html', context={ 'user_form': user_form, 'profile_form': profile_form, 'registered': registered, })
def register(request): registered = False 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 2 forms are valid... if user_form.is_valid() and profile_form.is_valid(): # save the users form data to the database user = user_form.save() # now we hash the password with set_password # 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 indicate that the template # registration was successful. registered = True else: print(user_form.errors, profile_form.errors) else: # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. user_form = UserForm() profile_form = UserProfileForm() # return the template depending on context return render( request, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def profile(request): if request.method == 'POST': # return HttpResponse('Dean') user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): username = request.POST.get('username') password = request.POST.get('password') email = request.POST.get('email') website = request.POST.get('website') picture = request.FILES['picture'] user = request.user id = request.user.id # return HttpResponse(user) userprofile = UserProfile.objects.get(user_id=id) userprofile.website = website if 'picture' in request.FILES: userprofile.picture = picture userprofile.save() user = User.objects.get(username=user) user.username = username user.email = email user.set_password(password) user.save() return HttpResponseRedirect('/') else: print user_form.errors, profile_form.errors # return HttpResponse('Armada') else: id = request.user.id username = request.user.username password = request.user.password email = request.user.email userprofile = UserProfile.objects.get(user_id=id) website = userprofile.website picture = userprofile.picture user_form = UserForm(initial={ 'username': username, 'password': password, 'email': email }) profile_form = UserProfileForm(initial={ 'website': website, 'picture': picture }) template = 'rango/profile.html' # return HttpResponse(password) context_dict = {'user_form': user_form, 'profile_form': profile_form} return render(request, template, context_dict)
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(): # 把 UserForm 中的数据存入数据库 user = user_form.save() # 使用set_password 方法计算密码哈希值 # 然后更新 user 对象 user.set_password(user.password) user.save() # 处理UserProfile 实例 # 因为要自行处理 user 属性,所以设定commit=False # 延迟保存模型,以防出现完整性问题 profile = profile_form.save(commit=False) profile.user = user # 头像文件 if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # 保存 profile.save() # 更新变量的值,告诉模板成功注册了 registered = True else: # 表单数据无效 # 打印问题 print(user_form.errors, profile_form.errors) else: # 不是 HTTP POST 请求, 渲染两个 ModelFrom 实例 # 表单为空,待用户填写 user_form = UserForm() profile_form = UserProfileForm() return render( request, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, registered: registered })
def register(request): # A boolean value # whether the registeration was secessful # true when registration succeeds registered = False # If a HTTP POST->process form data if request.method == 'POST': # Attempt to grab information from the raw form information # Make use of UserForm and UserProfileForm user_form = UserForm(request.POST) profile_form = UserProfileForm(request.POST) # if 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 the UserProfile instance # Set commit=False. This delays saving the model # Until we are ready to avoid integrity problems profile = profile_form.save(commit=False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Save the UserProfile model instance profile.save() # Update the variable and registeration was successful registered = True else: print(user_form.errors, profile_form.errors) else: # Not a HTTP POST, so we render our form using two ModelForm instance # These forms will be blank, reandy for user input user_form = UserForm() profile_form = UserProfileForm() return render(request, 'rango/register.html', context={ '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. registered = False # POST: processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. 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 user.set_password(user.password) user.save() # 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) # reference of the User instance in the UserProfile # populate the user attribute of the UserProfileForm form profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() # registration was successful. registered = True else: print(user_form.errors, profile_form.errors) else: # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render( request, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): # boolean telling the template if the registreation was succ # inital false, when registred turn to true registered = False #if itsa post we're intrested in processing form data if request.method == 'POST': # attempt to grab info from form user_form = UserForm(request.POST) profile_form = UserProfileForm(request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() # hash the password with set_password method # once hashed update the user object user.set_password(user.password) user.save() # we dont save to begin with as we need to add user, # do this later profile = profile_form.save(commit=False) profile.user = user # if the user provided a pic save it if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # now we save the user profile profile.save() # update vairable to show suer registered registered = True else: print(user_form.errors, profile_form.errors) else: #theyre not a user yet so we render the forms as blank user_form = UserForm() profile_form = UserProfileForm() # render the form depending on the context return render(request, 'rango/register.html', context = {'user_form':user_form, 'profile_form':profile_form, 'registered':registered})
def register(request): #Boolean for telling template whether the registration was succesful #False initially and changes when registration succeeds. registered = False #If HTTP POST we want to process form data if request.method == 'POST': #Try to grab info from form info user_form = UserForm(request.POST) profile_form = UserProfileForm(request.POST) #If two forms are valid... if user_form.is_valid() and profile_form.is_valid(): #Save user's form data to database user = user_form.save() #Now hash password with set_password method #Once hashed we can update user object user.set_password(user.password) user.save() #Now sort out UserProfile instance #Since we need to set user attribute ourselves, set commit=False which delays saving model until ready to avoid integrity problems profile = profile_form.save(commit=False) profile.user = user #Did user provide profile pic? If so need to get from input form and put in UserProfile model if 'picture' in request.FILES: profile.picture = request.FILES['picture'] #Now save UserProfile model instance profile.save() #Update variable to indicate template registration was succesful registered = True else: #Invalid form so print problems to terminal print(user_form.errors, profile_form.errors) else: #Not HTTP POST so render form using two ModelForm instances #These forms will be blank ready for user input user_form = UserForm() profile_form = UserProfileForm() #Render template depending on context return render(request, 'rango/register.html', context={ 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): #Boolean telling the template if the registration was successful. registered = False if request.method == 'POST': #Attempt to grab information from the raw form information. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) 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 we deal with the UserProfile instance. #Since we need to set the user attribute ourselves, #we set commit=False. profile = profile_form.save(commit=False) profile.user = user #Check if a profile picture was given. #Put it in the UserProfile model if there was. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] #Now we save the UserProfile model instance. profile.save() #Update out boolean to show that registration was successful. registered = True else: #Invalid form(s), print problems to the terminal. print(user_form.errors, profile_form.errors) else: #Not a HTTP POST, so we render our form using two ModelForm instances. #These forms will be blank, ready for user input. user_form = UserForm() profile_form = UserProfileForm() #Render the template depending on the context. return render( request, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): # 一个布尔值,告诉模板注册是否成功 # 一开始设为 False, 注册成功后改为 True registered = False # 如果是 HTTP POST 请求,处理表单数据 if request.method == 'POST': # 尝试获取原始表单数据 # 注意,UserForm 和 UserProfileForm 中的数据都需要 user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # 如果两个表单中的数据是有效的 if user_form.is_valid() and profile_form.is_valid(): # 把 UserForm 中的数据存入数据库 user = user_form.save() # 使用 set_password 方法计算密码哈希值 # 然后更行 user 对象 user.set_password(user.password) user.save() # 现在处理 UserProfile 实例 # 因为要自行处理 user 属性,所以设定 commit=False # 延迟保存模型,以防出现完整性问题 profile = profile_form.save(commit=False) profile.user = user # 用户提供了头像了吗 # 如果提供了,从表单数据库中提取出来, 赋给UserProfile 模型 if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # 保存实例 profile.save() # 更新变量的值,告诉模板注册成功了 registered = True else: print(user_form.errors, profile_form.errors) else: # 不是 POST 请求, 渲染两个 ModelForm 实例 # 表单为空,待用户填写 user_form = UserForm() profile_form = UserProfileForm() return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
def register(request): #A bool flag telling the template whether reg was successful, false initially registered = False #if http post we want to try process data if request.method == 'POST': #try grab info from raw form info (user and userprofileform) user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) #if both forms are valid if user_form.is_valid() and profile_form.is_valid(): #save users form data to db user = user_form.save() #now hash the password with set_password method and update object user.set_password(user.password) user.save() #now sort out userprofile isntance #need to set user attribute ourselves #set commit=false, this delays saving the model #until we're ready profile = profile_form.save(commit=False) profile.user = user #did the user give ap rofile pic? if so #we need to get it from the form and put #it in the UserProfile model if 'picture' in request.FILES: profile.picture = request.FILES['picture'] #now save the userpropfile model instance profile.save() #update our variable to indiciate that the template #registration was successful registered = True else: #invalid form or forms, mistakes etc? print errors to terminal print(user_form.errors, profile_form.errors) else: #not a http post so we render our form using two modelform instances #these forms will be blank, ready for user input user_form = UserForm() profile_form = UserProfileForm() #render the templat depending ont he context return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered':registered})
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(): # return model instance``self.instance``. 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'] # new we save the UserProfile model instance profile.save() 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, se we render out form using two modelform instances else: user_form = UserForm() profile_form = UserProfileForm() if request.session.test_cookie_worked(): print(">>>> TEST COOKIE WORKED!") request.session.delete_test_cookie() # Render the template depending on the context return render(request, 'rango/register.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. 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 user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) 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() # 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() # registration was successful registered = True else: # Invalid form or forms - mistakes or something else? print(user_form.errors, profile_form.errors) else: # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
def register(request): #Variable to indicate whether the registration was successful registered = False # If request == POST, we're interested in processing form data. if request.method == 'POST': #Grab info from raw form information user_form = UserForm(request.POST) profile_form = UserProfileForm(request.POST) if user_form.is_valid() and profile_form.is_valid(): #Save the user's form data to the database user = user_form.save() #Hash the password, update user object user.set_password(user.password) user.save() #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 pic? #If so put it in the UserProfile model if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: #Invalid form/s print(user_form.errors, profile_form.errors) else: #Not a HTTP POST, so we render out form using two ModelForm instances. #These forms will be blank, ready for user input user_form = UserForm() profile_form = UserProfileForm() #Render the template depending on the context return render(request, 'rango/register.html', context={ 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })