def post(self, request): edit = request.GET.get("edit") if edit: instance = Purchase.objects.get(id62=edit) form = PurchaseForm(request.POST, instance=instance) else: form = PurchaseForm(request.POST) if form.is_valid(): obj = form.save(commit=False) if edit: obj.updated_by = request.user else: obj.created_by = request.user obj.witnesses = form.cleaned_data['witnesses'] obj.facilities = form.cleaned_data['facilities'] obj.files = form.cleaned_data['files'] obj.save() messages.success( request, '%s (%s) has been saved.' % (obj.__class__.__name__, obj.__unicode__())) return redirect(reverse("app:purchase")) else: print form.errors return self.render_to_response({"form": form})
def purchase_save(request): response = reply_object() form = PurchaseForm(request.POST) if form.is_valid(): response = form.save() else: response["code"] = settings.APP_CODE["FORM ERROR"] response["errors"] = form.errors return HttpResponse(simplejson.dumps(response))
def purchase_item(request): form = PurchaseForm() if request.method == "POST": form = PurchaseForm(request.POST) if form.is_valid(): fd = form.save(commit=False) fd.buyer = request.user fd.save() messages.success(request, f"well done {fd.id} done") return redirect('home') messages.error(request, "errors") ctx = { 'form': form, 'price': 40, } return render(request, "purchase.html", ctx)