错误:view没有在Django中使用mixin在基于泛型类的视图中返回HttpResponse对象?

时间:2014-05-21 18:26:50

标签: django mixins django-class-based-views django-generic-views

我正在编写一个mixin,可以部分保存form。有关详细信息,请参阅我的previous post.。虽然这是固定的,但我有另一个错误。我认为错误来自post method混合而不是returnig HttpResponse Object。我正在进行super电话,应该注意这一点。但那没用。

错误:

Exception Type:     ValueError
Exception Value:    The view blog.views.ArticleCreateView didn't return an HttpResponse object.

这是 mixin:

class PendFormMixin(object):
    form_hash_name = 'form_hash'
    pend_button_name = 'pend'
    def get_form_kwargs(self):
        """
        Returns a dictionary of arguments to pass into the form instantiation.
        If resuming a pended form, this will retrieve data from the database.
        """
        form_hash = self.kwargs.get(self.form_hash_name)
        print "form_hash", form_hash
        if form_hash:
            import_path = self.get_import_path(self.get_form_class())
            return {'data': self.get_pended_data(import_path, form_hash)}
        else:
            print "called"
            # print super(PendFormMixin, self).get_form_kwargs()
            return super(PendFormMixin, self).get_form_kwargs()

    def post(self, request, *args, **kwargs):
        """
        Handles POST requests with form data. If the form was pended, it doesn't follow
        the normal flow, but saves the values for later instead.
        """
        self.object = None
        if self.pend_button_name in self.request.POST:
            print "here"
            form_class = self.get_form_class()
            print form_class
            form = self.get_form(form_class)
            # print "form is ", form
            self.form_pended(form)
            super(PendFormMixin, self).post(request, *args, **kwargs)
        else:
            super(PendFormMixin, self).post(request, *args, **kwargs)

# Custom methods follow
    def get_import_path(self, form_class):
        return '{0}.{1}'.format(form_class.__module__, form_class.__name__)

    def get_form_hash(self, form):
        content = ','.join('{0}:{1}'.format(n, form.data[n]) for n in form.fields.keys())
        return md5(content).hexdigest()

    def form_pended(self, form):
        import_path = self.get_import_path(self.get_form_class())
        form_hash = self.get_form_hash(form)
        print "in form_pended"
        pended_form = PendedForm.objects.get_or_create(form_class=import_path,
                                                       hash=form_hash)
        print "form_class", import_path
        print "form_hash", form_hash
        print "pended_form", pended_form
        for name in form.fields.keys():
            pended_form[0].data.get_or_create(name=name, value=form.data[name])
        print pended_form[0]
        return form_hash

    def get_pended_data(self, import_path, form_hash):
        data = PendedValue.objects.filter(import_path=import_path, form_hash=form_hash)
        return dict((d.name, d.value) for d in data)

以下是查看:

class ArticleCreateView(PendFormMixin, CreateView):
      form_class = ArticleForm
      model = Article
      template_name = "article_create.html"
      success_url = '/admin'

      def form_valid(self, form):
        """
        If the request is ajax, save the form and return a json response.
        Otherwise return super as expected.
        """
        if self.request.is_ajax():
            self.object = form.save()
            time.sleep(5)
            return HttpResponse(json.dumps("success"),
                mimetype="application/json")
        return super(ArticleCreateView, self).form_valid(form)

      def form_invalid(self, form):
        """
        We haz errors in the form. If ajax, return them as json.
        Otherwise, proceed as normal.
        """
        if self.request.is_ajax():
            return HttpResponseBadRequest(json.dumps(form.errors),
                mimetype="application/json")
        return super(ArticleCreateView, self).form_invalid(form)

2 个答案:

答案 0 :(得分:1)

调用super()不会使您的方法返回任何内容,它只调用超类。你应该在你的PendFormMixin.post()方法中使用return super(...)。

答案 1 :(得分:1)

基于类的视图的post方法应该返回一个HttpResponse对象(显然例外情况)。在你的mixin中,你忘了返回一些东西,因此错误。您只需要从CreateView电话中<{1}}返回结果:

super()