def update(request, category_id): if request.method == 'POST': category = Category.objects.get(id=category_id) form = CategoryForm(request.POST, instance=category) if form.is_valid(): form.save() return redirect('/categorias') else: return render_to_response('category_edit.html', locals(), context_instance=RequestContext(request))
def create(request): if request.method == 'POST': category = Category() form = CategoryForm(request.POST, instance=category) if form.is_valid(): form.save() return redirect('/categorias') else: return render_to_response('category_new.html', locals(), context_instance=RequestContext(request))
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)