def goods_detail(request): category_types = GoodsCategory.CATEGORY_TYPE if request.method == 'GET': return render(request, 'goods_detail.html', {'category_types': category_types}) if request.method == 'POST': form = GoodForm(request.POST) if form.is_valid(): # 如果表单验证的字段名和数据库的字段名相同可以使用如下方式添加 # data = form.cleaned_data # Goods.objects.create(**data) Goods.objects.create( name=form.cleaned_data.get('name'), goods_sn=form.cleaned_data.get('goods_sn'), goods_nums=request.POST.get('goods_nums'), market_price=request.POST.get('market_price'), shop_price=request.POST.get('shop_price'), goods_brief=form.cleaned_data.get('goods_brief'), category_id=form.cleaned_data.get('category'), goods_front_image=request.FILES.get('goods_front_image')) return HttpResponseRedirect(reverse('goods:goods_list')) else: return render(request, 'goods_detail.html', { 'form': form, 'category_types': category_types })
class GoodUpdate(PageNumberView, TemplateView, SortMixin, PageNumberMixin): good = None template_name = "good_edit.html" form = None formset = None def get(self, request, *args, **kwargs): self.good = Good.objects.get(pk = self.kwargs["pk"]) self.form = GoodForm(instance = self.good) self.formset = GoodImagesFormset(instance = self.good) return super(GoodUpdate, self).get(request, *args, **kwargs) def get_context_data(self, **kwargs): context = super(GoodUpdate, self).get_context_data(**kwargs) context["good"] = self.good context["form"] = self.form context["formset"] = self.formset return context def post(self, request, *args, **kwargs): self.good = Good.objects.get(pk = self.kwargs["pk"]) self.form = GoodForm(request.POST, request.FILES, instance = self.good) self.formset = GoodImagesFormset(request.POST, request.FILES, instance = self.good) if self.form.is_valid(): self.form.save() if self.formset.is_valid(): self.formset.save() messages.add_message(request, messages.SUCCESS, "Товар успешно изменен") return redirect(reverse("goods_index", kwargs = {"pk": self.good.genre.pk}) + "?page=" + self.request.GET["page"] + "&sort=" + self.request.GET["sort"] + "&order=" + self.request.GET["order"]) return super(GoodUpdate, self).get(request, *args, **kwargs)
class GoodUpdate(PageNumberView, TemplateView, SortMixin, PageNumberMixin): good = None template_name = "good_edit.html" form = None formset = None def get(self, request, *args, **kwargs): self.good = Good.objects.get(pk = self.kwargs["pk"]) self.form = GoodForm(instance = self.good) self.formset = GoodImagesFormset(instance = self.good) return super(GoodUpdate, self).get(request, *args, **kwargs) def get_context_data(self, **kwargs): context = super(GoodUpdate, self).get_context_data(**kwargs) context["good"] = self.good context["form"] = self.form context["formset"] = self.formset return context def post(self, request, *args, **kwargs): self.good = Good.objects.get(pk = self.kwargs["pk"]) self.form = GoodForm(request.POST, request.FILES, instance = self.good) self.formset = GoodImagesFormset(request.POST, request.FILES, instance = self.good) if self.form.is_valid(): self.form.save() if self.formset.is_valid(): self.formset.save() messages.add_message(request, messages.SUCCESS, "Товар успешно изменен") return redirect(reverse("goods_index", kwargs = {"pk": self.good.category.pk}) + "?page=" + self.request.GET["page"] + "&sort=" + self.request.GET["sort"] + "&order=" + self.request.GET["order"]) return super(GoodUpdate, self).get(request, *args, **kwargs)
class GoodCreate(PageNumberView, TemplateView, SortMixin, PageNumberMixin): template_name = "good_add.html" cat = None form = None formset = None def get(self, request, *args, **kwargs): if self.kwargs["pk"] == None: self.cat = Category.objects.first() else: self.cat = Category.objects.get(pk=self.kwargs["pk"]) self.form = GoodForm(initial={"category": self.cat}) self.formset = GoodImagesFormset() return super(GoodCreate, self).get(request, *args, **kwargs) def get_context_data(self, **kwargs): context = super(GoodCreate, self).get_context_data(**kwargs) context["category"] = self.cat context["form"] = self.form context["formset"] = self.formset return context def post(self, request, *args, **kwargs): self.form = GoodForm(request.POST, request.FILES) if self.form.is_valid(): new_good = self.form.save() self.formset = GoodImagesFormset(request.POST, request.FILES, instance=new_good) if self.formset.is_valid(): self.formset.save() messages.add_message(request, messages.SUCCESS, "Товар успешно добавлен") return redirect( reverse("goods_index", kwargs={"pk": new_good.category.pk}) + "?page=" + self.request.GET["page"] + "&sort=" + self.request.GET["sort"] + "&order=" + self.request.GET["order"]) if self.kwargs["pk"] == None: self.cat = Category.objects.first() else: self.cat = Category.objects.get(pk=self.kwargs["pk"]) self.formset = GoodImagesFormset(request.POST, request.FILES) return super(GoodCreate, self).get(request, *args, **kwargs)
class GoodCreate(PageNumberView, TemplateView, SortMixin, PageNumberMixin): template_name = 'good_add.html' cat = None form = None formset = None def get(self, request, *args, **kwargs): if self.kwargs['pk'] is None: self.cat = Category.objects.first() else: self.cat = Category.objects.get(pk=self.kwargs['pk']) self.form = GoodForm(initial={'category': self.cat}) self.formset = GoodImageFormset() return super().get(request, *args, **kwargs) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['category'] = self.cat context['form'] = self.form context['formset'] = self.formset return context def post(self, request, *args, **kwargs): self.form = GoodForm(request.POST, request.FILES) if self.form.is_valid(): new_good = self.form.save() self.formset = GoodImageFormset(request.POST, request.FILES, instance=new_good) if self.formset.is_valid(): self.formset.save() messages.add_message(request, messages.SUCCESS, 'Товар успешно добавлен') return redirect( reverse('goods_index', kwargs={'pk': new_good.category.pk}) + '?page=' + self.request.GET['page'] + '&sort=' + self.request.GET['sort'] + '&order=' + self.request.GET['order']) if self.kwargs['pk'] is None: self.cat = Category.objects.first() else: self.cat = Category.objects.get(pk=self.kwargs['pk']) self.formset = GoodImageFormset(request.POST, request.FILES) return super().get(request, *args, **kwargs)
class GoodUserCreate(TemplateView): template_name = None def get(self, request, *args, **kwargs): self.template_name = get_detect_platform_template( "goods/u_good/add.html", request.user, request.META['HTTP_USER_AGENT']) return super(GoodUserCreate, self).get(request, *args, **kwargs) def get_context_data(self, **kwargs): context = super(GoodUserCreate, self).get_context_data(**kwargs) context["form"] = GoodForm() context["sub_categories"] = GoodSubCategory.objects.only("id") context["categories"] = GoodCategory.objects.only("id") context["user"] = User.objects.get(pk=self.kwargs["pk"]) return context def post(self, request, *args, **kwargs): self.form = GoodForm(request.POST, request.FILES) self.user = User.objects.get(pk=self.kwargs["pk"]) if request.is_ajax() and self.form.is_valid(): from common.notify.notify import user_notify good = self.form.save(commit=False) new_good = good.create_good( title=good.title, image=good.image, images=request.POST.getlist('images'), list=good.list, sub_category=GoodSubCategory.objects.get( pk=request.POST.get('sub_category')), creator=request.user, description=good.description, price=good.price, comments_enabled=good.comments_enabled, votes_on=good.votes_on, community=None) return render_for_platform(request, 'goods/good_base/u_new_good.html', {'object': new_good}) else: return HttpResponseBadRequest()
class GoodCreate(PermissionRequiredMixin, PageNumberView, TemplateView, SortMixin, PageNumberMixin): permission_required = 'goods.add_good' template_name = "good_add.html" cat = None form = None formset = None def get(self, request, *args, **kwargs): if self.kwargs["pk"] is None: self.cat = Category.objects.first() else: self.cat = Category.objects.get(pk=self.kwargs["pk"]) self.form = GoodForm(initial={"category":self.cat}) self.formset = GoodImagesFormset() return super(GoodCreate, self).get(request, *args, **kwargs) def get_context_data(self, **kwargs): context = super(GoodCreate, self).get_context_data(**kwargs) context["category"] = self.cat context["form"] = self.form context["formset"] = self.formset return context def post(self, request, *args, **kwargs): self.form = GoodForm(request.POST, request.FILES) if self.form.is_valid(): new_good = self.form.save() self.formset = GoodImagesFormset(request.POST, request.FILES, instance=new_good) if self.formset.is_valid(): self.formset.save() messages.add_message(request, messages.SUCCESS, "Товар успешно добавлен") return redirect(reverse("goods_index", kwargs={"pk": new_good.category.pk}) + \ "?page=" + self.request.GET["page"] + "&sort=" + self.request.GET["sort"] + \ "&order=" + self.request.GET["order"]) if self.kwargs["pk"] is None: self.cat = Category.objects.first() else: self.cat = Category.objects.get(pk=self.kwargs["pk"]) self.formset = GoodImagesFormset(request.POST, request.FILES) return super(GoodCreate, self).get(request, *args, **kwargs)
class GoodUpdate(PageNumberView, TemplateView, SortMixin, PageNumberMixin): good = None template_name = 'good_edit.html' form = None formset = None def get(self, request, *args, **kwargs): self.good = Good.objects.get(pk=self.kwargs['pk']) self.form = GoodForm(instance=self.good) self.formset = GoodImageFormset(instance=self.good) return super().get(request, *args, *kwargs) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['good'] = self.good context['form'] = self.form context['formset'] = self.formset return context def post(self, request, *args, **kwargs): self.good = Good.objects.get(pk=self.kwargs['pk']) self.form = GoodForm(request.POST, request.FILES, instance=self.good) self.formset = GoodImageFormset(request.POST, request.FILES, instance=self.good) if self.form.is_valid(): self.form.save() if self.formset.is_valid(): self.formset.save() messages.add_message(request, messages.SUCCESS, 'Товар успешно изменен') return redirect( reverse('goods_index', kwargs={'pk': self.good.category.pk}) + '?page=' + self.request.GET['page'] + '&sort=' + self.request.GET['sort'] + '&order=' + self.request.GET['order']) return super().get(request, *args, **kwargs)
class GoodCommunityEdit(TemplateView): template_name = None def get(self,request,*args,**kwargs): self.good = Good.objects.get(pk=self.kwargs["pk"]) self.community = self.good.community self.template_name = get_community_manage_template("goods/c_good/edit.html", request.user, self.community, request.META['HTTP_USER_AGENT']) return super(GoodCommunityEdit,self).get(request,*args,**kwargs) def get_context_data(self,**kwargs): context = super(GoodCommunityEdit,self).get_context_data(**kwargs) context["form"] = GoodForm() context["sub_categories"] = GoodSubCategory.objects.only("id") context["categories"] = GoodCategory.objects.only("id") context["good"] = self.good context["community"] = self.good.community return context def post(self,request,*args,**kwargs): self.good = Good.objects.get(pk=self.kwargs["good_pk"]) self.form = GoodForm(request.POST,request.FILES, instance=self.good) if request.is_ajax() and self.form.is_valid() and request.user.pk.is_administrator_of_community(self.kwargs["pk"]): from common.notify.notify import user_notify good = self.form.save(commit=False) new_good = self.good.edit_good( title=good.title, image=good.image, images=request.POST.getlist('images'), list=good.list, sub_category=GoodSubCategory.objects.get(pk=request.POST.get('sub_category')), description=good.description, price=good.price, comments_enabled=good.comments_enabled, votes_on=good.votes_on) return render_for_platform(request, 'goods/good_base/c_new_good.html',{'object': new_good}) else: return HttpResponseBadRequest()