def add_profile(request): # A HTTP POST? if request.method == 'POST': form = UserProfileForm(request.POST) # Have we been provided with a valid form? if form.is_valid(): userProfile = form.save(commit=False) userProfile.user_id = request.user.id if 'picture' in request.FILES: userProfile.picture = request.FILES['picture'] # Save the new category to the database. form.save(commit=True) # Now call the index() viewed. # The user will be shown the homepage return index(request) else: # If the request was not a POST, display the form to enter details. form = UserProfileForm() # Bad form (or form details), no form supplied... # Render the form with error messages (if any). return render(request, 'rango/profile_registration.html',{'form':form})
def edit_profile(request): url = '/rango/' try: profile = request.user.userprofile except UserProfile.DoesNotExist: profile = UserProfile(user=request.user) if request.method == 'POST': profile_form = UserProfileForm(data=request.POST, instance=profile) if profile_form.is_valid(): profile_form.save(commit=False) if 'picture' in request.FILES: print "picture" profile.picture = request.FILES['picture'] profile.save() return redirect(url) else: print profile_form.errors else: profile_form = UserProfileForm(instance=profile) return render(request, 'rango/edit_profile.html', {'profile_form': profile_form} )
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 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 edit_profile(request): user = request.user profile = UserProfile.objects.get(id=user.id) if request.method == 'POST': profile_form = UserProfileForm(data=request.POST, instance=profile) if profile_form.is_valid(): profile_form.save() else: print profile_form.errors else: profile_form = UserProfileForm(instance=profile) return render(request, 'rango/edit_profile.html', {'profile_form': profile_form})
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): 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): 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 # 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) 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): # 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_profile(request): if request.method=='POST': user_id=request.POST['user_id'] old_profile_form=None try: old_profile_form=UserProfile.objects.get(user_id=user_id) except: pass profile_form=UserProfileForm(data=request.POST) user=None try: user=User.objects.get(pk=user_id) except: pass if user and profile_form.is_valid(): if not old_profile_form: profile=profile_form.save(commit=False) profile.user=user if 'picture' in request.FILES: profile.picture=request.FILES['picture'] profile.save() else: old_profile_form.website=request.POST['website'] if 'picture' in request.FILES: old_profile_form.picture=request.FILES['picture'] old_profile_form.save() return redirect('/rango/') else: print profile_form.errors return render(request,'rango/404.html',{}) else: profile_form=UserProfileForm() return render(request,'registration/profile_registration.html',{ 'profile_form':profile_form, })
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): # 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_profile(request): registered = False if request.method == 'POST': form = UserProfileForm(data = request.POST) if form.is_valid(): profile = form.save(commit=False) User = UserModel() # imported above current_user = User.objects.get(username = request.user.username) profile.user = current_user # form.user link comes from request object print profile.user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance to the DB profile.save() # Update our variable to tell the template registeration was succesful registered = True return index(request) else: print form.errors else: # If the request was not a POST, display the form to enter details # (i.e. upon first load) form = UserProfileForm() # Bad form (or form details), no form supplied... # Render the form with error messages (if any). return render(request, 'registration/profile_registration.html', {'form': form})
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_profile(request): if request.user.is_authenticated(): user = request.user registered = False if request.method == 'POST': profile_form = UserProfileForm(request.POST) if profile_form.is_valid(): 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 profile_form.errors else: profile_form = UserProfileForm() return render(request, 'registration/profile_registration.html', {'profile_form': profile_form, 'registered': registered})
def add_profile(request): username = '' #if method is post, we'll want to process this data if request.method == 'POST': profile_form = UserProfileForm(data=request.POST) if profile_form.is_valid(): profile = profile_form.save(commit=False) profile.user = request.user if request.user.is_authenticated(): username = request.user.get_username() if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() #add a redirect to the view_profile page when you make one else: print profile_form.errors else: profile_form = UserProfileForm() return render(request,'registration/profile_registration.html', {'profile_form': profile_form, 'username': username})
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): 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 ## 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 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() 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): #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() # 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): 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 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) 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): # 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(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 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', context={ '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() # Save the user's forms data to the database # 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() 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() 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.session.test_cookie_worked(): print ">>> TEST COOKIE WORKED!" request.session.delete_test_cookie() 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! Life is so beautiful.") 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() return render_to_response( 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered }, 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 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): #boolean value indicating whether the profile was successfully registered 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() #doesn't save until the connection between the user and user profile is created 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): # Boolean value to tell the template whether a registration was # successful. Initially set to false which can be changed when a # registration is successful 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 telling the template whether the registration was sucessful. 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_profile(request): if request.method == 'POST': try: profile = UserProfile.objects.get(user=request.user) form = UserProfileForm(request.POST, instance=profile) except: form = UserProfileForm(request.POST) if form.is_valid(): if request.user.is_authenticated(): profile = form.save(commit=False) user = User.objects.get(id=request.user.id) profile.user = user try: profile.picture = request.FILES['picture'] except: pass profile.save() else: print form.errors return index(request) else: form = UserProfileForm(request.GET) return render(request, 'rango/profile_register.html', {'profile_form': form})
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(): # 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'] 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 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() 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(request,'rango/register.html',context={'user_form':profile_form,'registered':registered})
def register(request): # if request.session.test_cookie_worked():#测试浏览器的cookies是否正常 # print(">>>> TEST COOKIE WORKED!") # request.session.delete_test_cookie() # else: # print('NO cookie') is_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() is_registered = True else: print(user_form.erros, 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':is_registered}) return render(request, 'rango/register.html', locals())
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 it's a http post, then we it meaans we're getting form data which we must in turn process! if request.method == 'POST': #try to grab the raw request data(binary?) user_form = UserForm(data = request.POST) profile_form = UserProfileForm(data = request.POST) #if the data from the two forms is valid.. if user_form.is_valid() and profile_form.is_valid(): #save the user FORM DATA to the database (but has it been processed yet?) user = user_form.save() #hash the password, once hashed we then create an user object !! user.set_password(user.password) user.save() #i.e. now we STORE it in the database; before in was just in the server's RAM or smtng #setting commit = false delays saving the mode until we are rid of integrity problems #create a variable which stores all the data frm the userProfile form... profile = profile_form.save(commit=False) #flesh the 1 to 1 relationship i.e. fix the userprofiles's user associate profile.user = user #if the user provided a pic in the registration form.. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: #one of the form (or both) is invalid; print the errors to terminal print(user_form.errors, profile_form.errors) else: #if it's not a post, actually...then it is a GET! sooo let's display stuff 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): ### testowanie ciastek, patrz index view #if request.session.test_cookie_worked(): #print ">>> TEST COOKIE WORKED!" #request.session.delete_test_cookie() context = RequestContext(request) cat_list = get_category_list() # 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 UseForm 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 any 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_to_response( 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered, 'cat_list': cat_list }, context)
def edit_profile(request, user_profile_id): try: user = User.objects.get(id=user_profile_id) except User.DoesNotExist: user = None context_dict = {} error_dictionaries = {} has_error = False if user is None: error_dictionaries[ 'user_lookup_error'] = 'user id {0} could not be found.'.format( str(user_profile_id)) print('user id {0} could not be found.'.format(str(user_profile_id))) has_error = True if user.id != request.user.id: error_dictionaries[ 'user_mismatch_error'] = 'user id and request user id are different.' print('user id and request user id are different.') has_error = True user_profile = None try: if has_error is False: user_profile = UserProfile.objects.get(user=user) except UserProfile.DoesNotExist: print('No Profile found for user {0}'.format(str(user.username))) has_error = True if request.method == 'POST' and has_error is not True: # In order to update data we already have, # we need to bind the form object to the instance we retrieved from the back-end! # https://stackoverflow.com/questions/26651688/django-integrity-error-unique-constraint-failed-user-profile-user-id#26652053 form = UserProfileForm(request.POST, request.FILES or None, instance=user_profile) # NOTE: to read the value of a check-box it must have a name and a value. # When the widget is 'checked' the value set in HTML is passed to this view for processing. # Otherwise, (eg no checked) the field is not provided at all. Hence python variable will be None clear_image = request.POST.get('clear_image') if form.is_valid(): # TODO: this seems to allow for changing user profiles willyneely the_profile = form.save(commit=False) if clear_image is not None and '' != clear_image: # SNAG: this does not allow the overload in the Model to delete the underlying file # On the plus side, if the user clears the default, it does not delete the default image file! the_profile.picture = None # This code updates the image correctly, but does not remove the orphaned one. # Removal is done in the save overload of the model. the_profile.save() # Redirects to the user's profile page return profile(request, user_profile_id) else: error_dictionaries['validation_errors'] = form.errors else: # print('Allowing {0} to edit his/her data.'.format(str(user.username))) form = UserProfileForm() if has_error: return HttpResponseRedirect("/") # Something might have gone wrong, but at least messages are directed to the correct user! context_dict['form'] = form context_dict['user_profile'] = user_profile context_dict['error_dictionaries'] = error_dictionaries context_dict['has_error'] = has_error context_dict['user_profile_id'] = user_profile_id return render(request, 'rango/edit_profile.html', context_dict)
def register(request): #get the request context context = RequestContext(request) #set value to store the registartion status. Initialy False it will change #to True after succesfully resitration proces registered = False #check if it's POST if request.method == 'POST': #get the user information that are provided in concrete fileds user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) #check if the form is valid if user_form.is_valid() and profile_form.is_valid(): #save user to the database user = user_form.save() #hash the user password by use of set_password method user.set_password(user.password) user.save() #deal with user profile (website and pictures) #get the profile but not save it until it will succesfully be made profile = profile_form.save(commit=False) profile.user = user #check if picture is provided picture = request.FILES.get('picture', False) if picture: #if picture is pass it to the user profile profile.picture = picture else: profile.picture = None #now we can save the profile profile.save() #update the registration info registered = True else: print user_form.errors, profile_form.errors #it's not the POST method so simply render the registartion page #using the two model instatnces else: user_form = UserForm() profile_form = UserProfileForm() #create context dictionary to hold site content context_dict = { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered, 'categories': get_category_list() } return render_to_response('registration.html', context_dict, context)
def register(request): # this boolean variable informs whether the registration was successful or not. # Initially False and when the registration succeeds its changed to True. registered = False # if HTTP method is POST then were interested in processing form data if request.method == 'POST': # attempt to retrieve info from the raw form information using both UserForm # and UserProfileForm user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # if the 2 forms are valid then if user_form.is_valid() and profile_form.is_valid(): # save all the user form data to the database user = user_form.save() # now we hash the password using the set_password method # once hashed we can update the user object user.set_password(user.password) user.save() # now we need to sort out the user profile instance for the user # since we need to set the user attributes ourselves we set commit = false # which delays saving the model until we are readt to avoid integrity problems profile = profile_form.save(commit=False) # assign the user instance that has been just created to the user field # in the profile model to indicate that the user that has just been created # is the one that this profile instance is associated with. profile.user = user # check to see if the user submitted a profile picture and if so we need to get # it from the input form and put it in the UserProfile model if 'picture' in request.FILES: # save it to the database model for the user profile profile.picture = request.FILES['picture'] profile.save() # update the registered variable to inform that the # template registration was successful. registered = True else: # otherwise print the relevant error messages stating why the form wasnt valid print(user_form.errors, profile_form.errors) # otherwise the request must have been a get request in which the user has not submitted # the form yet, so we render our form using two ModelForm instances. These forms will be # blank allowing the user to input their details. In the render supply the # html request, the template used and the context dictionary which holds the 2 forms # and the registered boolean variable that will be set to true. else: user_form = UserForm() profile_form = UserProfileForm() return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form,
def register(request, username_url=None): context = RequestContext(request) # Tells register.html if registration has succeeded or not. registered = False # These keep track of user information in the case of an edit. oldUser = None oldPassword = None oldPicture = None # Find the user that is currently being edited. if username_url != None: users = UserProfile.objects.all() for each_user in users: if each_user.getUsername() == username_url: oldUser = each_user oldPassword = oldUser.user.password oldPicture = oldUser.picture # If the user has submitted the form, process it. if request.method == 'POST': if oldUser: # Change the username of the old user so that vaidation works. oldUser.user.username = '******' + oldUser.user.username oldUser.user.save() # 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(): if oldUser: oldUser.user.delete() oldUser.delete() # then save the user's form data. user = user_form.save() # Password is hashed. if oldUser: # If this is an update to an existing user, if user.password != '*******': # and the password is updated, user.set_password(user.password) # change it. user.save() elif user.password == '*******': # Otherwise, user.password = oldPassword # set is as the old password. user.save() else: user.set_password(user.password) user.save() 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'] elif oldUser: profile.picture = oldPicture profile.save() # 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 # Change the username back. if oldUser: oldUser.user.username = oldUser.user.username[1:] oldUser.user.save() # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: if oldUser: user_form = UserForm({ 'username': oldUser.user.username, 'email': oldUser.user.email, 'password': '******' }) profile_form = UserProfileForm({ 'first_name': oldUser.first_name, 'last_name': oldUser.last_name, 'website': oldUser.website, 'picture': oldUser.picture, }) 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, 'username_url': username_url }, context)
def register(request): #if request.session.test_cookie_worked(): TEST THE COOKIES WORKING # print(">>> Test Cookie worked!") # request.session.delete_test_cookie() # A boolean value for telling the template whether the registration was succesful # Set to False initially. Code changes value to True when registration succedsself. registred = False # if it's a HTTP POST, we're interested in procesing form data if request.method == 'POST': # Attempt to grab infromation from the raw form information # Note that we make use of both Useform 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['pictures'] # Now we save the UserProfile model isntance profile.save() #Update our variable to tell the template registration was succesful. registred = True # Invalid form of 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 usign two ModelForm instances. # These forms will be blank, ready for the 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, 'registred': registred })
from django.http import HttpResponseRedirect, HttpResponse
context_dict = {'form': form, 'category': category} return render(request, 'rango/add_page.html', context=context_dict) 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): 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 user_login(request): # If the request is a HTTP POST, try to pull out the relevant information. if request.method == 'POST': # Gather the username and password provided by the user. # This information is obtained from the login form. username = request.POST['username'] password = request.POST['password'] # Use Django's machinery to attempt to see if the username/password # combination is valid - a User object is returned if it is. user = authenticate(username=username, password=password) # If we have a User object, the details are correct. # If None (Python's way of representing the absence of a value), no user # with matching credentials was found. if user: # Is the account active? It could have been disabled. if user.is_active: # If the account is valid and active, we can log the user in. # We'll send the user back to the homepage. login(request, user) return HttpResponseRedirect('/rango/') else: # An inactive account was used - no logging in! return HttpResponse("Your Rango account is disabled.") else: # Bad login details were provided. So we can't log the user in. print "Invalid login details: {0}, {1}".format(username, password) return HttpResponse("Invalid login details supplied.") # The request is not a HTTP POST, so display the login form. # This scenario would most likely be a HTTP GET. else: # No context variables to pass to the template system, hence the # blank dictionary object... return render(request, 'rango/login.html', {}) @login_required def restricted(request): return HttpResponse("Since you're logged in, you can see this text!") # Use the login_required() decorator to ensure only those logged in can access the view. @login_required def user_logout(request): # Since we know the user is logged in, we can now just log them out. logout(request) # Take the user back to the homepage. return HttpResponseRedirect('/rango/') def search(request): result_list = [] if request.method == 'POST': query = request.POST['query'].strip() if query: # Run our Bing function to get the results list! result_list = run_query(query) return render(request, 'rango/search.html', {'result_list': result_list}) def track_url(request): page_id = None url = '/rango/' if request.method == 'GET': if 'page_id' in request.GET: page_id = request.GET['page_id'] try: page = Page.objects.get(id=page_id) page.views = page.views + 1 page.save() url = page.url except: pass return redirect(url)
def register(request): #A boolean value telling template whether reg. was success #set false orig. code change value to true when reg. succeed registered = False #if HTTP POST, we process form data if request.method == 'POST': #try to grab info from raw form info #note: use of 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(): 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 the USerProfile instance #need to set the user attribute ourselves, #set commit=False. This delays #saving the model until we're ready. #this avoids integrity problems. profile = profile_form.save(commit=False) #After creating a new User model instance #we reference it in the UserProfile instance #with the line below: profile.user = user #This is where we populate the user attribute #of the UserProfile form, #which we hid from users. #check if user provided picture #if so get it from input form #put it in UserProfile model if 'picture' in request.FILES: profile.picture = request.FILES['picture'] #save the UserProfile model instance profile.save() #update variable to indicate the the template reg. #was successful. registered = True else: #invalid form - print to terminal print(user_form.errors, profile_form.errors) else: # not HTTP POST, render our form using #two model form instances #these will be blank ready for #user input... huh... what does that mean??? user_form = UserForm() profile_form = UserProfileForm() #render the template depending on 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(): # Сохранить данные формы пользователя в базу данных. user = user_form.save() # Теперь мы хэшируем пароль с помощью метода set_password. # После хеширования мы можем обновить объект user. 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) else: # Не HTTP POST, поэтому мы визуализируем нашу форму, # используя два экземпляра ModelForm. # Эти формы будут пустыми, готовыми для ввода пользователем. user_form = UserForm() profile_form = UserProfileForm() # Визуализировать шаблон в зависимости от контекста. return render( request, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })