def add_user(request): if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): form.save(commit=True) return index(request) else: print form.errors else: form = UserForm() return render(request, 'rango/add_user.html', {'form': form})
def AddUser(request): template = 'rango/add_user.html' if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): form.save(commit=True) return HttpResponseRedirect('/') else: print form.errors else: form = UserForm() return render(request, template, {'form': form})
def profile(request): User = request.user context = RequestContext(request) if request.method == 'POST': user_form = UserForm(request.POST, request.FILES) if user_form.is_valid(): user_form.save() messages.success(request, 'Profile Details Updated') else: messages.success(request, 'Profile Details NOT Updated') else: user_form = UserForm() return render_to_response('profile.html', {'user': User, 'user_form': user_form}, 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): 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() 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): # 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): # 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 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() # 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 context_dict = {} if request.method == "POST": user_form = UserForm(request.POST) profile_form = UserProfileForm(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 'image' in request.FILES: profile.image = request.FILES['image'] 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 context_dict['profile_form'] = profile_form context_dict['registered'] = registered return render(request, 'rango/register.html',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(): # 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 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(commit=False) user.set_password(user.password) user.save() user = User.objects.get(username=user.username) 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): 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 } cat_list = get_category_list() context_dict['cat_list'] = cat_list return render(request, 'rango/register.html', context_dict, context)
def register(request): registered = False user_form = UserForm() userprofile_form = UserProfileForm() if request.method == 'POST': user_form = UserForm(request.POST) userprofile_form = UserProfileForm(request.POST) if user_form.is_valid() and userprofile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.is_active = True user.is_staff = True user.save() userprofile = userprofile_form.save(commit=False) userprofile.user = user if 'picture' in request.FILES: userprofile.picture = request.FILES['picture'] userprofile.save() registered = True else: print user_form.errors,userprofile_form.errors context_dict = {'user_form': user_form, 'userprofile_form': userprofile_form, 'registered': registered} return render(request, 'rango/register.html', context=context_dict)
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): #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): #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(request.POST) profile_form = UserProfileForm(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', context={'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
def register(request): registered = False # If set to true the form is filled in. 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() # set password method and save it. profile = profile_form.save(commit=False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() # user model instance saved. 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): # 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): 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() 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): 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 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): 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): registered = False if request.method == 'POST': #如果是提交表格 将数据转换为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() 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): 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() #hash the password with the set_password method user.set_password(user.password) user.save() profile = profile_form.save(commit=False) # link 2 forms together profile.user = user #check if there is picture 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() 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): 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): 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): 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): # 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): 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): 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): 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): 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 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': 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) 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 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): 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): # 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 the 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 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 indicate that the template registration was successful 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 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 test_create_user_using_view(self): """ test user creation functions in views.py (no form used)""" # delete user if it exists try: userKK = User.objects.get(username=username) userKK.delete() except User.DoesNotExist: pass # create user self.username with password self.passwd # fill user form loginDict = {} loginDict["username"] = username loginDict["email"] = email loginDict["password"] = passwd userForm = UserForm(data=loginDict) # fill profile form profileDict = {} profileDict['website'] = website # create auxiliary black image for profileForm filesDict = {'picture': createPicture()} userProfileForm = UserProfileForm(data=profileDict, files=filesDict) if userForm.is_valid() and userProfileForm.is_valid(): try: # Save the user's form data to the database. user = userForm.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() # we need to set a user object in userprofile userprofile = userProfileForm.save(commit=False) userprofile.user = user userprofile.save() except Exception as e: raise Exception("Error processing form: %s" % e.message) self.assertTrue(userForm.is_valid()) self.assertTrue(userProfileForm.is_valid()) # check user name try: userKK = User.objects.get(username=username) except User.DoesNotExist: userKK = User() self.assertEqual(userKK.email, email) # check file name try: userprofileKK = UserProfile.objects.get(user=userKK) except User.DoesNotExist: userprofileKK = UserProfile() imageName = join(settings.MEDIA_ROOT, userprofileKK.picture.name) with Image.open(imageName) as im: width, height = im.size self.assertEqual(width, 200)
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 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): #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. 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): # 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 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): #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): #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): # 用来返回给模版文件注册是否成功的标识变量。 # 初始设置为 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 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): # 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) 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 # 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_to_response( 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)
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): #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})