form_valid =的CreateView +重新定义没有返回HttpResponse对象

时间:2016-08-13 10:41:04

标签: django django-views django-generic-views

Django 1.10 强文

你能帮助我理解为什么会导致:

视图wiki.views.WikiCreate未返回HttpResponse对象。它返回了None。

帖子:重新定义post方法时,我没有干扰继承链。我只是从网址拦截了参数。

form_valid:我返回了HttpResponse。但似乎某个地方后来它消失了。

urlpatterns = [
    url(r'^(?P<model>[-\w]+)/(?P<pk>\d+)/wiki/create/$', WikiCreate.as_view(), name='wiki_create'),
]



class WikiCreate(CreateView):
    model = Wiki
    fields = ['article']

    def post(self, request, *args, **kwargs):
        #For wiki history.
        self.author = request.user

        # Parameters from url reflect fields not shown to user.
        self.related_model = kwargs.get('model')
        self.related_object_id = kwargs.get('pk')

        super(WikiCreate, self).post(request, *args, **kwargs)

    def form_valid(self, form):
        """
        Only "article" field was shown to tue user.
        Now we save fields that were not explicitly shown (but were in the url).
        """
        self.object = form.save(commit=False)
        self.object.related_model = self.related_model
        self.object.related_id = self.related_object_id
        self.object.save()
        return HttpResponseRedirect(self.get_success_url())

1 个答案:

答案 0 :(得分:0)

您没有从post中的超级电话中返回响应。

请注意,覆盖该方法并不是一个好主意,而且您当然不需要这样做。您可以直接在form_valid

中访问这些kwargs