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 the registration succeeds. registered = False # If it's a HTTP POST, we're interested in processing form data if request.method == 'POST': # Attempt to grab informaton 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 fields 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, # 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 save the UserProfile model instance. profile.save() # Update our variable to indicate that thet 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 if request.method == 'POST': user_form = UserForm(data = request.POST) profile_form = UserProfileForm(data = request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit = False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileForm() return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
def register(request): if request.session.test_cookie_worked(): print(">>>> TEST COOKIE WORKED!") request.session.delete_test_cookie() context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print(user_form.errors, profile_form.errors) else: user_form = UserForm() profile_form = UserProfileForm() context_dict = { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered } cat_list = get_category_list() context_dict['cat_list'] = cat_list return render(request, 'rango/register.html', context_dict, context)
def register(request): 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() & 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 profile.save() else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render(request,'rango/register.html',{'user_form': user_form,'profile_form': profile_form,'register': register})
def register(request): context = RequestContext(request) registered = False if request.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): 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): 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(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() context_dict = dict() 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 # to check whether the user is already registered 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(request,'rango/register.html',context_dict)
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): # 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 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() context_dict = {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} # Render the template depending on the context. return render(request,'registration/registration_form.html',context_dict)
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 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): # Request the context. context = RequestContext(request) cat_list = get_category_list() context_dict = {} context_dict['cat_list'] = cat_list # Boolean telling us whether registration was successful or not. # Initially False; presume it was a failure until proven otherwise! registered = False # If HTTP POST, we wish to process form data and create an account. if request.method == 'POST': # Grab raw form data - making use of both FormModels. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # Two valid forms? if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data. That one is easy. user = user_form.save() # Now a user account exists, we hash the password with the set_password() method. # Then we can update the account with .save(). user.set_password(user.password) user.save() # Now we can sort out the UserProfile instance. # We'll be setting values for the instance ourselves, so commit=False prevents Django from saving the instance automatically. profile = profile_form.save(commit=False) profile.user = user # Profile picture supplied? If so, we put it in the new UserProfile. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the model instance! profile.save() # We can say registration was successful. registered = True # Invalid form(s) - just print errors to the terminal. else: print user_form.errors, profile_form.errors # Not a HTTP POST, so we render the two ModelForms to allow a user to input their data. else: user_form = UserForm() profile_form = UserProfileForm() context_dict['user_form'] = user_form context_dict['profile_form'] = profile_form context_dict['registered'] = registered # Render and return! return render_to_response('rango/register.html', context_dict, context)
def register(request): # A boolean value for telling the template whether the registration was successful. # Set to False initially. Code changes value to True when registration succeeds registered = False # If its a HTTP POST, we're interested in processing form data if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the user's form data to the database user = user_form.save() # Now we hash the password with the set_password method # Once hashed, we can update the user object user.set_password(user.password) user.save() # Sort out the user profile instance # Since we need to set the user attribute outselves, we set commit = False. # This delays ssaving the model until we're ready to avoid integrity problems profile = profile_form.save(commit=False) profile.user = user # Did user provide a profile picture? If so, need to get it from the input # form and put it in the UserProfile model if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance profile.save() # update our variable to indicate that the template registration was successful registered = True else: # invalid form or forms = mistakes or something else? # print problems to the terminal print(user_form.errors, profile_form.errors) else: # Not a http post, so we render our form using 2 ModelForm instances # These forms will be blank, ready for user input user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context return render( request, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): #A boolean value 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): # Логическое значение указывающее шаблону прошла ли регистрация успешно. # В начале ему присвоено значение False. Код изменяет значение на True, если регистрация прошла успешно. registered = False # Если это HTTP POST, мы заинтересованы в обработке данных формы. if request.method == 'POST': # Попытка извлечь необработанную информацию из формы. # Заметьте, что мы используем UserForm и UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # Если в две формы введены правильные данные... if user_form.is_valid() and profile_form.is_valid(): # Сохранение данных формы с информацией о пользователе в базу данных. user = user_form.save() # Теперь мы хэшируем пароль с помощью метода set_password. # После хэширования мы можем обновить объект "пользователь". user.set_password(user.password) user.save() # Теперь разберемся с экземпляром UserProfile. # Поскольку мы должны сами назначить атрибут пользователя, необходимо приравнять commit=False. # Это отложит сохранение модели, чтобы избежать проблем целостности. profile = profile_form.save(commit=False) profile.user = user # Предоставил ли пользователь изображение для профиля? # Если да, необходимо извлечь его из формы и поместить в модель UserProfile. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Теперь мы сохраним экземпляр модели UserProfile. profile.save() # Обновляем нашу переменную, чтобы указать, что регистрация прошла успешно. registered = True # Неправильная формы или формы - ошибки или ещё какая-нибудь проблема? # Вывести проблемы в терминал. # Они будут также показаны пользователю. else: print(user_form.errors, profile_form.errors) # Не HTTP POST запрос, следователь мы выводим нашу форму, используя два экземпляра ModelForm. # Эти формы будут не заполненными и готовы к вводу данных от пользователя. else: user_form = UserForm() profile_form = UserProfileForm() # Выводим шаблон в зависимости от контекста. return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered} )
def register(request): #boolean to tell template true if registration succeeds. registered = False #if it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': #attempt to grab info from the raw form information. #need make user of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) #if the two forms are valid. if user_form.is_valid() and profile_form.is_valid(): #save user's form data to the database. user = user_form.save() #hash password for security, then update the user object. user.set_password(user.password) user.save() #to sort out Userprofile instance. #since we need to set the user attributes ourselves, #we set commit=False. This delays saving the model #until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user #user provided a dp? #true, get it from input form and put it in the userprofile model if 'picture' in request.FILES: profile.picture = request.FILES['picture'] #now save the userprofile model instance. profile.save() #update our variable to indicate that the template #registration was successful. registered = True else: #invalid form ot forms - mistakes or soemthing else? #print problems to the terminal. print(user_form.errors, profile_form.errors) else: #not a HTTP POSt, so we render our form using two modelform instances. #these forms will be blank, ready for user input. user_form = UserForm() profile_form = UserProfileForm() #ender the template depending on the context. return render( request, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): #boolean was registration successful? registered = False #if http post, we want to process form data if request.method == 'POST': #attempt to grab information from the raw form info #note that we make user of both UserForm and UserProfileForm user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) #if two forms are valid if user_form.is_valid() and profile_form.is_valid(): #save users form to db user = user_form.save() #now we hash password with set_password method #once hashed, we can update the user object user.set_password(user.password) user.save() #now sort out UserProfile instance #since we need to set the user attribute ourselves, we set #commit=False. this delays saving model until we're ready #to avoid integrity problems profile = profile_form.save(commit=False) profile.user = user #did the user provide a profile picture? #if yes, get it from input form and put in userprofile model if 'picture' in request.FILES: profile.picture = request.FILES['picture'] #now save UserProfile model instance profile.save() #update our variable to indicate that the template registration #was successful registered = True else: #invalid form or forms, print problems to terminal print(user_form.errors, profile_form.errors) else: #not http post, so we render our form using two ModelForm instances #these forms will be blank, ready for user input user_form = UserForm() profile_form = UserProfileForm() #render the template depending on the context return render(request, 'rango/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
def register(request): # 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): # 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 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): # 用来返回给模版文件注册是否成功的标识变量。 # 初始设置为 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): #Bool for login (Changes to true when logged in) registered = False # If it's a HTTP POST, we're interested in processing form data. if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() #Hash and save password user.set_password(user.password) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, we set commit=False. # This delays saving the model until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user #If we have a profile picture, deal with it if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() #Update registered = True # Invalid form or forms - mistakes or something else? # Print problems to the terminal. # They'll also be shown to the user. else: print user_form.errors, profile_form.errors # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. else: user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render( request, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): # 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): # Boolean telling whether the registration was successful registered = False if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(request.POST) profile_form = UserProfileForm(request.POST) # If both of the forms are valid... if user_form.is_valid() and profile_form.is_valid(): # Save the form data to the database user = user_form.save() # set_password method is used for hashing # once hashed, update the object user.set_password(user.password) user.save() # now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, # we set commit=False. This delays saving the model # until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user # Did the user provide a profile picture? # If so, we need to get it form the input form and # put it into the UserProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: # Invalid form or forms, print problems to the terminal print(user_form.errors, profile_form.errors) else: # Not a HTTP POST, so we render our form using two ModelForm instances. user_form = UserForm() profile_form = UserProfileForm() # Render the template depending on the context. return render(request, 'rango/register.html', context={ 'user_form': user_form, 'profile_form': profile_form, 'registered': registered, })
def register(request): registered = False if request.method == 'POST': # Attempt to grab information from the raw form information. # Note that we make use of both UserForm and UserProfileForm. user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) # if the 2 forms are valid... if user_form.is_valid() and profile_form.is_valid(): # save the users form data to the database user = user_form.save() # now we hash the password with set_password # once hashed, we can update the user object user.set_password(user.password) user.save() # Now sort out the UserProfile instance. # Since we need to set the user attribute ourselves, # we set commit=False. This delays saving the model # until we're ready to avoid integrity problems. profile = profile_form.save(commit=False) profile.user = user # Did the user provide a profile picture? # If so, we need to get it from the input form and # put it in the UserProfile model. if 'picture' in request.FILES: profile.picture = request.FILES['picture'] # Now we save the UserProfile model instance. profile.save() # Update our variable to indicate that the template # registration was successful. registered = True else: print(user_form.errors, profile_form.errors) else: # Not a HTTP POST, so we render our form using two ModelForm instances. # These forms will be blank, ready for user input. user_form = UserForm() profile_form = UserProfileForm() # return the template depending on context return render( request, 'rango/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })