def add(request): if request.method == 'POST': # if method was post (form submittion), fill form from post data form = ServiceForm(request.POST) form_valid = form.is_valid() formset = DiscountFormSet(request.POST) formset_valid = formset.is_valid() if form_valid and formset_valid: # if form is valid, save it and redirect back to myservices # commit=False tells form to not save the object to the database just yet and return it instead service = form.save(commit=False) # set service_provider to the current user before we save the object to the database service.service_provider = request.user.service_provider service.save() formset.instance = service formset.save() return HttpResponseRedirect(reverse(myservices)) else: # on get request create empty form form = ServiceForm(instance=request.user) formset = DiscountFormSet() # render form - new (get request) or invalid with error messages (post request) return render_to_response('service/add.html', locals(), context_instance=RequestContext(request))
def edit(request, id): service = get_object_or_404(Service, service_provider=request.user.service_provider, id=id) if request.method == 'POST': form = ServiceForm(request.POST, instance=service) form_valid = form.is_valid() formset = DiscountFormSet(request.POST, instance=service) formset_valid = formset.is_valid() if form_valid and formset_valid: form.save() formset.save() return HttpResponseRedirect(reverse(myservices)) else: form = ServiceForm(instance=service) formset = DiscountFormSet(instance=service) return render_to_response('service/edit.html', locals(), context_instance=RequestContext(request))
def add(request): if request.method == 'POST': # if method was post (form submittion), fill form from post data form = ServiceForm(request.POST) form_valid = form.is_valid() formset = DiscountFormSet(request.POST) formset_valid = formset.is_valid() if form_valid and formset_valid: # if form is valid, save it and redirect back to myservices # commit=False tells form to not save the object to the database just yet and return it instead service = form.save(commit=False) # set service_provider to the current user before we save the object to the database service.service_provider = request.user.service_provider service.save() formset.instance = service formset.save() return HttpResponseRedirect(reverse(myservices)) else: # on get request create empty form form = ServiceForm() formset = DiscountFormSet() # render form - new (get request) or invalid with error messages (post request) return render_to_response('service/add.html', locals(), context_instance=RequestContext(request))