def add_category(request): context = RequestContext(request) category_list = get_5_categories() if request.user.is_active and request.method == 'POST': #print request.method #print request.POST.copy() form = CategoryForm(request.POST) if form.is_valid(): form.save(commit=True) return index(request) else: print form.errors else: #print request.method #print request.GET.copy() form = CategoryForm context_dict = {'form': form, 'categories': category_list} return render_to_response('rango/add_category.html', context_dict, context)
def add_category(request): # Get the context from the request. context = RequestContext(request) # A HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # Have we been provided with a valid form? if form.is_valid(): # Save the new category to the database. form.save(commit=True) # Now call the index() view. # The user will be shown the homepage. return index(request) else: # The supplied form contained errors - just print them to the terminal. print form.errors else: # If the request was not a POST, display the form to enter details. form = CategoryForm() # Bad form (or form details), no form supplied... # Render the form with error messages (if any). return render_to_response('rango/add_category.html', {'form': form}, context)
def add_category(request): """ User must be logged in to use this functionality. Adds a new category corresponding to the information in the form filled by the user. If form empty, display form. If form filled and valid, create a new category. If form filled and not valid, display form errors. """ # An HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # Have we been provided with a valid form? if form.is_valid(): # Save the new category to the database. form.save(commit=True) # Now call the index() view. # The user will be shown the homepage. return index(request) else: # The supplied form contained errors - just print them to the terminal. #return HttpResponse(form.errors) #return index(request) print form.errors else: # If the request was not a POST, display the form to enter details. form = CategoryForm() # Bad form (or form details), no form supplied... # Render the form with error messages (if any). return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): # get context from request context = RequestContext(request) if request.method == 'POST': form = CategoryForm(request.POST) # check if form is valid if form.is_valid(): # save new category to db form.save(commit=True) # now call the index() view # the user will be shown the homepage return index(request) else: # form had errors print form.errors else: # non-POST requests should display form to user form = CategoryForm() # bad form (or form details), no form supplied... # render form with error messages (if any) return render_to_response( 'rango/add_category.html', {'form': form}, context)
def add_category(request): # Get the context from the request. context = RequestContext(request) cat_list = get_category_list() context_dict = {} context_dict['cat_list'] = cat_list # A HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # Have we been provided with a valid form? if form.is_valid(): # Save the new category to the database. form.save(commit=True) # Now call the index() view. # The user will be shown the homepage. return index(request) else: # No form passed - ignore and keep going. pass else: # If the request was not a POST, display the form to enter details. form = CategoryForm() # Bad form (or form details), no form supplied... # Render the form with error messages (if any). context_dict['form'] = form return render_to_response('rango/add_category.html', context_dict, context)
def add_category(request): context = RequestContext(request) #Check the html message type if request.method == 'POST' : form = CategoryForm(request.POST) #Validate the form if form.is_valid(): #Save the new object form.save(commit=True) #Redirect the user to the index return index(request) else: #Print errors on terminal print form.errors else: #Get the form attributes form = CategoryForm() #Bad Form (or form details) #Render the form with error messages return render_to_response('rango/add_category.html', { 'form':form, 'cat_list': get_category_list() }, context)
def add_category(request): #get the context context = RequestContext(request) #create empty error dictionary errors = '' #check if POST if request.method == 'POST': form = CategoryForm(request.POST) #check if form is valid if form.is_valid(): #save the new category to the database form.save(commit=True) #go back to the main page return index(request) else: # form contains some error extract them errors = form.errors else: # request is GET simply display the form to the user form = CategoryForm() #create dictionary with site context context_dict = {'form' : form, 'errors' : errors, 'categories' : get_category_list()} return render_to_response('add_category.html',context_dict, context)
def add_category(request): context = RequestContext(request) cat_list = get_category_list() #HTTP POST if request.method == 'POST': form = CategoryForm(request.POST) #Have we been provided with a valid form if form.is_valid(): #save new category to database form.save(commit=True) #return to index view. #User will now see the homepage return index(request) else: #the supplied form contains errors - print these to terminal print form.errors else: #if the request was not a POST, display the form to enter details form = CategoryForm() # Bad form (or form details), no form supplied... # Render the form with error messages (if any). return render_to_response('rango/add_category.html', {'form': form, 'cat_list': cat_list}, context)
def add_category(request): # A HTTP Post? if request.method == 'POST': form = CategoryForm(request.POST) # Have we been provided with a valid form? if form.is_valid(): # Save the new category to the database. form.save(commit=True) # cat = form.save(commit=True) # print cat, cat.slug # Now call the index() view. # The user will be shown the homepage return index(request) else: # The supplied form contained errors - just print them to the terminal print form.errors else: # If the request was not a POST, display the form to enter details. form = CategoryForm() # Bad form (or form details), no form supplied .. # Render the form with error messages (if any). return render(request, './rango/add_category.html', { 'form': form })
def add_category(request): # A HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # Have we been provided with a valid form? if form.is_valid(): # Save the new category to the database. form.save(commit=True) # Now call the index() view. # The user will be shown the homepage. return index(request) else: # If the supplied form contained errors, making it NOT valid, print these # errors to the terminal print form.errors else: # If the request was not a POST, display the form to enter details # (i.e. upon first load) form = CategoryForm() # Bad form (or form details), no form supplied... # Render the form with error messages (if any). return render(request, 'rango/add_category.html', {'form': form})
def add_category(req): """View used to add new categories""" # Get the context from the request. context = RequestContext(req) # Retrieve categories list for the left navbar cat_list = get_category_list() # A HTTP POST? If so, provide a form for posting a new category if req.method == 'POST': form = CategoryForm(req.POST) # Have we been provided with a valid form input from the user? if form.is_valid(): # Save the new category to the database form.save(commit=True) # Now call the index() view. # The user will be served with the index.html template return index(req) else: # The supplied form contained errors - just print them to the terminal print form.errors else: # If the request wasn't a POST, display the form to enter details form = CategoryForm() context_dict = {'cat_list': cat_list, 'form': form} # Bad form (or form details), no form supplied... # Render the form with error messages (if any). return render_to_response('rango/add_category.html', context_dict, context)
def add_category(request): # HTTP Post if request.method == 'POST': form = CategoryForm(request.POST) # Is form valid? if form.is_valid(): # Save category to database form.save(commit=True) # Call index and go to homepage return index(request) # If not valid form else: # Form errors (printed to terminal) print(form.errors) # HTTP Get elif request.method == 'GET': # Create form form = CategoryForm # Render form - include any error messages return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): context = RequestContext(request) #Checking whether the submit type is POST or not (becauase the form is submitted on POST) if request.method == 'POST': #Process the form data form = CategoryForm(request.POST) #Check the validity if form.is_valid(): #Save the new category to our SQLite 3 Database form.save(commit=True) #Now load it up on the Index view as the new Category is saved...Easy! return index(request) else: #if containingf errors, print it out to the terminal )(will replace after dev) print form.errors else: #If not post , then display the form to the user.....(As both Post and get are dpne to the same thread unlike PHP) form = CategoryForm() #Invoked only when NOT SUCCESS return render_to_response('rango/add_category.html' , {"form" :form} , context)
def add_category(request): #get the context form the request context = RequestContext(request) #A HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) #have them provided with a valid form if form.is_valid(): form.save(commit=True) #now call the main() view #the user will be shown the homepage return main(request) else: print form.errors else: form = CategoryForm() #bad form of form details, no from suppied #render the form with error msgs if any return render_to_response('rango/add_category.html', {'form':form}, context)
def add_category(request): # Get the context from the request. context = RequestContext(request) cat_list = get_category_list() # A HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # Have we been provided with a valid form? if form.is_valid(): # Save the new category to the database. form.save(commit=True) # Now call the index() view. # The user will be shown the homepage. return index(request) else: # The supplied form contained errors - just print them to the terminal. print form.errors # According to REST principles, if we didn't use POST, it has to be a GET, then display a form to submit it afterwards. else: # If the request was not a POST, display the form to enter details. form = CategoryForm() # Bad form (or form details), no form supplied... # Render the form with error messages (if any). return render_to_response('rango/add_category.html', {'form': form, 'cat_list': cat_list}, context)
def add_category(request): # Get the context context = RequestContext(request) # A HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # have we been provided with a valud form if form.is_valid(): # Save the new category to the database form.save(commit=True) # redirect to homepage return index(request) else: print form.errors else: # If the request was not a POST, display the form to enter details. form = CategoryForm() context_dict = {'form' : form} context_dict['cat_list'] = get_cat_list() # Bad form (or form details), no form supplied... # Render the form with error messages (if any). return render_to_response('rango/add_category.html', context_dict, context)
def add_page(request, category_name_url): context = RequestContext(request) category_name = decode_url(category_name_url) if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): form.save(commit=False) try: cat = Category.objects.get(name=category_name) page.category = cat except Category.DoesNotExist: return render_to_response('rango/add_page.html', context_dict, context, context) page.views = 0 page.save() return category(request, category_name_url) else: print form.errors else: form = PageForm() return render_to_response( 'rango/add_page.html', {'category_name_url': category_name_url, 'category_name': category_name, 'form': form}, context)
def add_category(request): form = CategoryForm() # A HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # Have we been provided with a valid form? if form.is_valid(): # Save the new category to the database. form.save(commit=True) # Now that the category is saved # We could give a confirmation message # But since the most recent category added is on the index page # then we can redirect the user back to the index page. return index(request) else: # The supplied form contained errors - # just print them to the terminal. print(form.errors) # Will handle the bad form, new form or # no form supplied cases. # Render the form with error messages (if any). return render(request, 'rango/add_category.html', {'form': form})
def add_category(request, placeholder=''): # HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # valid form provided? if form.is_valid(): # Save the new category to the database form.save(commit=True) # Now call the index() view. # The user will be shown the homepage # return index(request) deprecated return redirect('rango:index') else: # The supplied form contained errors print(form.errors) else: # If the request was not a POST, display the form to enter details. if placeholder == '': form = CategoryForm() else: form = CategoryForm(initial={ 'name' : placeholder }) # form = CategoryForm() return render(request, 'rango/add_category.html', {'form':form})
def add_category(request): form = CategoryForm() if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): form.save(commit=True) return index(request) else: print(form.errors) return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): if request.method == "POST": form = CategoryForm(request.POST) if form.is_valid(): form.save(commit=True) return index(request) else: print form.errors else: form = CategoryForm() return render(request, "rango/add_category.html", {"form": form})
def add_category(request): if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): form.save(commit=True) return HttpResponseRedirect('/rango/') else: print form.errors else: form = CategoryForm() return render(request, 'rango/add_category.html', {'form': form})
def addCategory(request): template = "rango/addCategory.html" if request.method == "GET": return render(request, template, {"form": CategoryForm()}) # request.method=='POST' form = CategoryForm(request.POST) if not form.is_valid(): return render(request, template, {"form": form}) form.save(commit=True) return rango(request) # Call function rango()
def add_category(request): context = RequestContext(request) if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): form.save(commit=True) return index(request) else: print form.errors else: form = CategoryForm() return render_to_response('rango/add_category.html',{'form':form}, context)
def add_category(request): if request.method == "POST": form = CategoryForm(request.POST) if form.is_valid(): form.save(commit = True) return redirect('/',request = request) else: print form.errors print "I have printed errors !\n" return render(request,'rango/add_category.html',{'form':form}) else: form = CategoryForm() return render(request,'rango/add_category.html',{'form':form})
def add_category(request): # read the form data if we have a POST request form = CategoryForm(request.POST or None) context = {'form': form,} if form.is_valid(): form.save(commit=True) return HttpResponseRedirect(reverse('rango:index')) else: print(form.errors) return render(request, 'rango/add_category.html', context)
def add_category(request): # A HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): # Save the new category to the database form.save(commit=True) # Will be shown the homepage return index(request) else: print(form.errors) else: form = CategoryForm() return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): form.save(commit=True) # return to homepage return index(request) else: print form.errors else: # display form form = CategoryForm() return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): if request.method == 'POST': form = CategoryForm(request.POST) # try: if form.is_valid(): form.save(commit=True) return index(request) else: print form.errors # except Exception as e: # print e else: form = CategoryForm() return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): context = RequestContext(request) if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): form.save(commit=True) return index(request) else: print form.errors else: # if request was a GET, display form to enter details. form = CategoryForm() return render_to_response('rango/add_category.html', {'form': form, 'cat_list': get_category_list() }, context)
def add_category(request): form = CategoryForm() # A HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # Have we been provided with a valid form? if form.is_valid(): # Save the new category to the database. form.save(commit=True) # Could provide a confirmation message but for this project # the category will be added to the index page. # Redirect the user to the index page. return index(request) else: # The supplied form contains errors. print(form.errors) # Will handle the bad form, new form, or no form supplied cases. # Render the form with error messages if any. return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): # A HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # Have we been provided with a valid form? if form.is_valid(): # Save the new category to the database. form.save(commit=True) # Now call the index() view. # The user will be shown the homepage. return add_page(request) else: # The supplied form contained errors - just print them to the terminal. print form.errors else: # If the request was not a POST, display the form to enter details. form = CategoryForm() # Bad form (or form details), no form supplied... # Render the form with error messages (if any). return render(request, 'rango/add_category.html/', {'form': form})
def add_category(request): form = CategoryForm() if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): category = form.save(commit=True) print category, category.slug return index(request) else: print form.errors return render(request, 'rango/add_category.html', {'form':form})
def add_category(request): form = CategoryForm() context_dict = {} # Is the request POST, i.e. has the user submitted the form? if request.method == 'POST': form = CategoryForm(request.POST) # We only save the form if it is valid, otherwise # print out errors in terminal if form.is_valid(): form.save(commit=True) return index(request) else: print(form.errors) context_dict["form"] = form print(type(context_dict)) # If the request is not POST, it is GET and we display # the appropriate template with the form return render(request, 'rango/add_category.html', context_dict)
def add_category(request): form = CategoryForm() try: if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): form.save(commit=True) return index(request) else: print form.errors return render(request, 'rango/add_category.html', {'form': form}) except IntegrityError as e: return render(request, "rango/add_category.html", { 'form': form, "message": e.message })
def add_category(request): context = RequestContext(request) cat_list = get_category_list() if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): #save the new category to the database. form.save(commit=True) return index(request) else: # print the errors to the terminal print(form.errors) else: # If the request was not a POST, display the form form = CategoryForm() # Bad form (or form details), no form supplied... # Render the form with error messages (if any). return render_to_response('rango/add_category.html', { 'form': form, 'cat_list': cat_list }, context)
def add_category(request): form = CategoryForm() # Chek if the HTTP request is a POST - did the user submit data via the form? if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): # Save the new category to the database form.save(commit=True) # cat = form.save(commit=True) # print(cat, cat.slug) # Now that the category is saved, we could confirm this # For now, just redirect the user back to the index view return redirect('/rango/') else: # If the supplied form contained errors... print(form.errors) # just print to the terminal # Handle the bad form, new form, or no form supplied cases # Render the form eith error messages (if any) return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): context = RequestContext(request) cat_list = get_category_list() context_dict = {} context_dict['cat_list'] = cat_list if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): form.save(commit=True) return index(request) else: print form.errors else: form = CategoryForm() context_dict['form'] = form return render_to_response('rango/add_category.html', context_dict, context)
def add_category(request): form = CategoryForm() if request.method == "POST": form = CategoryForm(request.POST) if form.is_valid(): cat = form.save(commit=True) return index(request) else: print(form.errors) return render(request, "rango/add_category.html", {'form': form})
def add_category(request): form = CategoryForm() if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): cat = form.save(commit=True) print('New category added: ', cat, '(' + cat.slug + ')') return index(request) else: print(form.errors) return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): form = CategoryForm() #An HTTP Post? if request.method == 'POST': form = CategoryForm(request.POST) #Provided w/ a valid form? if form.is_valid(): #save new cat. to DB category = form.save(commit=True) form.save(category, category.slug) #now that it's saved, give confirm. message #Since cat. added is on index, redirect to index page return index(request) else: #supplied form has errors, print to terminal print(form.errors) # will handle bad form, new form, or no form cases. # render form w/ any error messages return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): form = CategoryForm() if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): cat = form.save(commit=False) cat.save() return index(request) else: print(form.errors) return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): # A HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # Have we been provided with a valid form? if form.is_valid(): # Save the new category to the database. form.save(commit=True) # Now call the index() view. # The user will be shown the homepage return index(request) else: # The supploed form contained errors - just print them to the terminal. print form.errors elif request.method == 'GET': form = CategoryForm() if 'add_page' in request.GET and 'add_url' in request.GET: add_page = request.GET.get('add_page') add_url = request.GET.get('add_url') category_name = request.GET.get('category_name') category_name = Category.objects.get_or_create( name=category_name)[0] page = Page.objects.get_or_create(title=add_page, url=add_url, category=category_name)[0] page.save() return HttpResponseRedirect('/') #return HttpResponsere(add_page) else: # If the request was not a POST, display the form to enter details. form = CategoryForm() # Bad form(or form details), no form supplied... # Render the form with error message (if any). template = 'rango/add_category.html' context_dict = {'form': form} return render(request, template, context_dict)
def add_category(request): form = CategoryForm() if request.method == 'POST': form = CategoryForm(request.POST) # Have we been provided with a valid form? if form.is_valid(): # Save the new category to the database. form.save(commit=True) # Now that the category is saved, we could confirm this. # For now, just redirect the user back to the index view. return redirect(reverse('rango:index')) else: # The supplied form contained errors - # just print them to the terminal. print(form.errors) # Will handle the bad form, new form, or no form supplied cases. # Render the form with error messages (if any). return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): form = CategoryForm() # A HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # Have we been provided with a valid form? if form.is_valid(): # Save the new category to the database. form.save(commit=True) # Now that the category is saved # We could give a confirmation message # But since the most recent category added is on the index page # Then we can direct the user back to the index page. return index(request) else: # The supplied form contained errors - # just print them to the terminal. print(form.errors) # Will handle the bad form, new form, or no form supplied cases. # Render the form with error messages (if any). return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): form = CategoryForm() if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): cat = form.save(commit=True) print(cat.slug) return redirect('/rango/') else: print(form.errors) return render(request, 'rango/add_category.html', {'form': form})
@ login_required def add_category(request): form = CategoryForm() # A HTTP POST? - this checks to see if the HTTP request was a POST request, # which if it was, means that the user submitted data via the form. # if this is not the case then the next statement executed will be the rendering of the view # with the template and context dictionary displaying the empty form to the user for them to # enter their data. if request.method == 'POST': form = CategoryForm(request.POST) # Check to see if the form is valid if form.is_valid(): # save the new category to the database form.save(commit=True) # Now the category is saved we could give a confirmation message, # but since the most recent category added is on the index page we # can redirect the user back to the index page so they can see their # form has been submitted successfully. return index(request) else: # The supplied form must contain some errors # These therefore should be printed to the terminal print(form.errors) # This will handle the bad form, new form, or no form supplied cases. # Render the form with error messages (if any) supplying the template that will # be used within the view and the context didctionary. # This redirects the user to the view but displays an error message to the # user allowing them to resubmit after fixing the error, or gives a blank form # allowing them to enter their data.
def add_category(request): form = CategoryForm() # HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # Valid form? if form.is_valid(): # Save to database form.save(commit=True) # We can go anywhere from here, a confirmation msg, or to some other page. # Since the newest categories are shown at the index page, we'll go there. return index(request) else: # Form was invalid, print errors to terminal. print(form.errors) # Get to here: form isnt valid and complete # This return will handle all cases: bad form, new form, missing cases. # Render the form with error messages too (if any). return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): context = RequestContext(request) # A HTTP post? if request.method == "POST": form = CategoryForm(request.POST) # is this a valid form? if form.is_valid: # save the category to the database. form.save(commit = True) # call index() view and redirect user back to homepage. return index(request) else: #the form contains errors. print these to the terminal. print form.errors else: # if request not POST, must be GET, display form form = CategoryForm() # display an empty form return render_to_response('rango/add_category.html',{'form':form},context)
def add_category(request): form = CategoryForm() # 是 HTTP POST 请求吗? if request.method == 'POST': # 检查是不是HTTP POST请求,即是不是通过表单提交的数据 form = CategoryForm(request.POST) # 表单数据有效吗? if form.is_valid(): # 把新分类存入数据库 form.save(commit=True) # 保存新分类后可以显示一个确认信息 # 不过既然最受欢迎的分类在首页 # 那就把用户带到首页吧 return index(request) else: # 表单数据有错误 # 直接在终端里打印出来 print(form.error) # 处理有效数据和无效数据之后 # 渲染表单,并显示可能出现的错误信息 return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): # a HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # is this a valid form? if form.is_valid(): form.save(commit=True) #send the user back to the index page return index(request) else: # if the form isn't valid, display the errors print form.errors else: # if it wasn't a POST request, show the form to the user form = CategoryForm() # If the form (or the form details) is bad or no form supplied # Render the form with any error messages return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): form = CategoryForm() # Check if HTTP request was a POST if request.method == 'POST': form = CategoryForm(request.POST) # If provided with a valid form # save the new category to the database if form.is_valid(): form.save(commit=True) # direct user back to index page after # category is saved return index(request) else: # If supplied form contains errors, # print these to the terminal print(form.errors) # Handle the bad form, new form or no form supplied cases # and render with error messages if any return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): # HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # Все поля формы были заполнены правильно? if form.is_valid(): # Сохранить новую категорию в базе данных. form.save(commit=True) # Теперь вызвать предсталвение index(). # Пользователю будет показана главная страница. return index(request) else: # Обрабатываемая форма содержит ошибки - вывести их в терминал. print(form.errors) else: # Если запрос был не POST, вывести форму, чтобы можно было ввести в неё данные. form = CategoryForm() # Форма с ошибкой (или ошибка с данных), форма не была получена... # Вывести форму с сообщениями об ошибках (если они были). return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): if request.user.is_authenticated(): form = CategoryForm() if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): cat = form.save(commit=True) print(cat, cat.slug) return index(request) else: print(form.errors) return render(request, 'rango/add_category.html', {'form': form}) else: return HttpResponse("You are not logged in.")
def add_category(request): form = CategoryForm() if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): # Save new category to database cat = form.save(commit=True) print(cat) # New category will show up on the index page return index(request) else: print(form.errors) return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): context = RequestContext(request) # HTTP POST or GET if request.method == 'POST': form = CategoryForm(request.POST) # Is the form valid? if form.is_valid(): # Saving the new category form.save(commit=True) # Return to the Home Page return index(request) else: # Print the errors print form.errors else: # If the request was not a POST, display the form to enter details form = CategoryForm() return render_to_response('rango/add_category.html', {'form': form}, context)
def add_category(request): form = CategoryForm() # A HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # Нам предоставили действительную форму? if form.is_valid(): # Сохранить новую категорию в базе данных. form.save(commit=True) # Теперь, когда категория сохранена, # мы можем выдать подтверждающее сообщение, # но поскольку последняя добавленная категория # находится на странице индекса, то мы можем # направить пользователя обратно на страницу индекса. return index(request) else: # В предоставленной форме есть ошибки - # просто распечатайте их в терминал print(form.errors) # Будет обрабатывать плохую форму, новую форму или не предоставленные формы. # Рендеринг формы с сообщениями об ошибках (если есть). return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): #a http post? if request.method == 'POST': form = CategoryForm(request.POST) #have we been provided with a valid form? if form.is_valid(): #save new category to database. form.save(commit=True) #print cat, cat.slug #now call the index() view. #user will be shown the homepage. return index(request) else: #the supplied form contained errors - print them to the terminal. print form.errors else: #if request was not a POST, display form to enter details. form = CategoryForm() #bad form (or form details), no form supplied ... #render the form with error messages (if any). return render(request, 'rango/add_category.html', {'form': form})
def post(self, request): form = CategoryForm(request.POST) if form.is_valid(): cat = form.save(commit=True) # confirmation that the category is added print(cat, cat.slug) return redirect(reverse('rango:index')) else: # print form contained errors to the terminal print(form.errors) return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): form = CategoryForm() # A HTTP POST ? if request.method == 'POST': form = CategoryForm(request.POST) # have we been provided with a valid form if form.is_valid(): #save the new category to the database form.save(commit=True) #now that the category is saved, we confirm this #for now, just redirect the user bakc to index view return redirect('/rango/') else: #if the form isnt valid, show errors print(form.errors) #will handle the bad form, new form, or no form supplied cases # render the form with error message return render(request, 'rango/add_category.html', {'form': form})