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): 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): # immediately get the context - as it may contain posting data context = RequestContext(request) if request.method == 'POST': # data has been entered into the form via Post form = CategoryForm(request.POST) if form.is_valid(): # the form has been correctly filled in, # so lets save the data to the model cat = form.save(commit=True) # show the index page with the list of categories return index(request) else: # the form contains errors, # show the form again, with error messages pass else: # a GET request was made, so we simply show a blank/empty form. form = CategoryForm() # pass on the context, and the form data. return render_to_response('rango/add_category.html', {'form': form }, context)