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_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): # 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 test_blank_data(self): """ Checks form with blank data and corresponding form errors. """ form = CategoryForm(data={}) self.assertFalse(form.is_valid()) self.assertEqual(form.errors, {'name': ['This field is required.']})
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. 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() context_dict = {'form': form} # Bad form (or form details), no form supplied... # Render the form with error messages (if any). return render(request, 'rango/add_category.html', context_dict)
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): # 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): context_dict = {} print('[debug] what call is this? {}'.format(request.method)) # A HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # Provided with a valid form? if form.is_valid(): # Save the new category to the database cat = form.save(commit=True) print('[debug] {} - {}'.format(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 (on submit), 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(request, 'rango/add_category.html', context_dict)
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): """ 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): 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, 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): 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): 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_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): #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): # 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): 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): 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): #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): 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(): cat = form.save() return redirect('category', cat.slug) 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 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): 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 post(self, request): form = CategoryForm(request.POST) # if the form valid? if form.is_valid(): form.save(commit=True) # redirect back to index return redirect(reverse('rango:index')) else: # form contained errors # print them to the terminal 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(): #Save the new category to the database 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(): try: cat = form.save(commit = True) return index(request) except IntegrityError: form.add_error('name', 'Category with that Name already exists.') else: pass 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: form = CategoryForm() return render_to_response('rango/add_category.html', {'form': form}, context)
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): # A HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) # Have we been provided with a valid form? 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, }, RequestContext(request))
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() if request.method == "POST": form = CategoryForm(request.POST) if form.is_valid(): form.save(commit=True) return redirect("/rango/") 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 = Category() cat.name = form.cleaned_data['name'] cat.save() messages.success(request, 'Category '+cat.name+' successfully included! ') return redirect('index') else: messages.error(request, 'Category not included, invalid form!') 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(cat, cat.slug) 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 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): form = CategoryForm() if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): form.save(commit=True) #return index(request) return HttpResponseRedirect(reverse('index')) else: print(form.errors) 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(): form.save(commit=True) return redirect('/rango/') else: print(form.errors) return render(request, 'rango/add_category.html', {'form': form}) else: return redirect(reverse('rango:login'))
def add_category(request): form = CategoryForm() if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): form.save(commit=True) return redirect('/rango/') else: print(form.errors) return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): # import pdb # pdb.set_trace() if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): form.save(commit=True) return HttpResponseRedirect('/rango/') # vs. index(request) else: print form.errors else: 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(): try: form.save(commit=True) return index(request) except IntegrityError: form.add_error("name", "Category with this Name already exists.") print(form.errors) else: print(form.errors) return render(request, 'rango_t/add_category.html', context={'form': form})
def add_category(request): form = CategoryForm() # A http post? if request.method == 'POST': form = CategoryForm(request.POST) # Check validity if form.is_valid(): form.save(commit=True) # Save to database 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) print('post请求, 有效, 内容是%s' % request.body) return index(request) else: print(form.errors) else: print('这是GET请求啊') form = CategoryForm return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): #creates a CategoryForm instance form = CategoryForm() #if user input data if request.method == 'POST': form = CategoryForm(request.POST) #Adds category to database if form is valid, then returns to index if form.is_valid(): form.save(commit=True) return index(request) else: print(form.errors) #print errors in terminal return render(request, 'rango/add_category.html', {'form': form})
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(): 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('rango') 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()): # save the form if it is valid form.save(commit=True) # redirect the user to the homepage return index(request) else: print(form.errors) 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) return index(request) else: print(form.errors) # Will handle the bad form, new form, or no form supplied cases. 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) #What's commit? #return index(request) return HttpResponseRedirect(reverse('rango:index')) else: print form.errors else: form = CategoryForm() context_dict = {'form': form} cat_list = get_cat_list() context_dict['cat_list'] = cat_list return render(request, 'rango/add_category.html', context_dict)
def add_category(request): form = CategoryForm() # Received HTTP POST? if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): form.save(commit=True) # save new category to DB return index(request) # redirect to index page else: # supplied form contains errors 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(): try: form.save(commit=True) return index(request) except IntegrityError as e: print('kharrrr', e.__cause__) return render(request, 'rango/add_category.html', {'form': form, 'custom_error': 'Category with this Name allready exists'}) 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 the form is valid if form.is_valid(): # save the category to the database form.save(commit=True) # redirect the user back to the index view return redirect('/rango/') else: print(form.errors) return render(request, 'rango/add_category.html', {'form': form})
def add_category(request): form = CategoryForm() context_dict = {} # A HTTP Method if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): form.save(commit=True) return index(request) else: print(form.errors) context_dict['form'] = form return render(request, 'rango/add_category.html', context_dict)
def add_category(request): form = CategoryForm() if request.method == 'POST': form = CategoryForm(request.POST) if form.is_valid(): 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('/rango/') 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(): form.save(commit=True) # since the category is saved..and it is shown on index page..redirect it to index return index(request) else: print(form.errors) # in case of bad entried forms re-render the form with correspondind error messages return render(request, 'rango/add_category.html', {'form': form})