无法覆盖post方法UpdateView

时间:2015-07-27 13:56:21

标签: django

我尝试覆盖generic.UpdateView的post方法,以便从其他模型添加一些表单。没有覆盖的帖子它工作正常。

class DesktopView(LoginRequiredMixin, UpdateView):
    model = WeddyUser
    context_object_name = 'weddyuser'
    slug_field = 'username'
    form_class = WeddyuserForm

    def get_context_data(self, **kwargs):
        context = super(DesktopView, self).get_context_data(**kwargs)
        if self.request.user.is_vendor:
            if 'form' not in context:
                context['form'] = self.form_class(self.request.GET, instance=self.request.user)
            context['vendor'] = Vendor.objects.get(id=self.request.user.id)
        else:
            context['plainuser'] = PlainUser.objects.get(id=self.request.user.id)
        return context

    def get(self, request, *args, **kwargs):
        self.object = self.get_object()
        c = {}
        c.update(csrf(request))
        user = request.user
        if self.kwargs['slug'] == request.user.username:
            if user.is_vendor:
                return render_to_response('user/vendor_edit_profile.html', self.get_context_data(),
                                      RequestContext(request, c))
            return render_to_response('user/user_edit_profile.html', self.get_context_data(),
                                  RequestContext(request, c))
        return HttpResponse(status=404)

    def post(self, request, *args, **kwargs):
        self.object = self.get_object()
        c={}
        c.update(csrf(request))
        form = self.form_class(request.POST)
        if form.is_valid():
            print ('ITS WORK')
            form.save()
            return render_to_response('user/vendor_edit_profile.html', self.get_context_data(form=form),
                                      RequestContext(request, c))
        else:
            print(form.errors)
            return render_to_response('user/vendor_edit_profile.html', self.get_context_data(form=form),
                                      RequestContext(request, c))

每次我收到无效表格 - “必填字段”

1 个答案:

答案 0 :(得分:0)

你不应该压倒在这里获取或发布。在您的情况下,您需要为post中的逻辑覆盖form_valid,并为get中的逻辑覆盖get_tempmlate