def post(self, request): data = request.POST.copy() data["transfer"] = eval(data["transfer"]) * settings.GB form = GoodsForm(data) if form.is_valid(): form.save() messages.success(request, "数据更新成功!", extra_tags="添加成功") return HttpResponseRedirect(reverse("sspanel:admin_goods")) else: messages.error(request, "数据填写错误", extra_tags="错误") context = {"form": form} return render(request, "my_admin/good_create.html", context=context)
def post(self, request, pk): good = Goods.objects.get(pk=pk) data = request.POST.copy() data["transfer"] = eval(data["transfer"]) * settings.GB form = GoodsForm(data, instance=good) if form.is_valid(): form.save() messages.success(request, "数据更新成功", extra_tags="修改成功") return HttpResponseRedirect(reverse("sspanel:admin_goods")) else: messages.error(request, "数据填写错误", extra_tags="错误") context = {"form": form, "good": good} return render(request, "my_admin/good_detail.html", context=context)
def good_create(request): """商品创建""" if request.method == "POST": # 转换为GB data = request.POST.copy() data["transfer"] = eval(data["transfer"]) * settings.GB form = GoodsForm(data) if form.is_valid(): form.save() messages.success(request, "数据更新成功!", extra_tags="添加成功") return HttpResponseRedirect(reverse("sspanel:backend_shop")) else: messages.error(request, "数据填写错误", extra_tags="错误") context = {"form": form} return render(request, "backend/goodcreate.html", context=context) else: form = GoodsForm() return render(request, "backend/goodcreate.html", context={"form": form})
def good_edit(request, pk): """商品编辑""" good = Goods.objects.get(pk=pk) # 当为post请求时,修改数据 if request.method == "POST": # 转换为GB data = request.POST.copy() data["transfer"] = eval(data["transfer"]) * settings.GB form = GoodsForm(data, instance=good) if form.is_valid(): form.save() messages.success(request, "数据更新成功", extra_tags="修改成功") return HttpResponseRedirect(reverse("sspanel:backend_shop")) else: messages.error(request, "数据填写错误", extra_tags="错误") context = {"form": form, "good": good} return render(request, "backend/goodedit.html", context=context) # 当请求不是post时,渲染form else: data = {"transfer": round(good.transfer / settings.GB)} form = GoodsForm(initial=data, instance=good) context = {"form": form, "good": good} return render(request, "backend/goodedit.html", context=context)