Django CreateView没有返回HttpResponse对象

时间:2017-11-14 10:55:05

标签: python django django-views django-class-based-views

无法弄清楚为什么CreateView不会返回HttpResponse。现在,我使用此视图仅用于发布(无GET)。我认为集合self.success_url应该足够了(正如您在def post中看到的那样)。

class TripCreationView(CreateView):
    form_class = TripCreationForm
    template_name = 'frontend/homepage.html'

    def post(self, request, *args, **kwargs):
        self.success_url = request.POST.get('success_url') or reverse('frontend:homepage')
        super(TripCreationView, self).post(self, request, *args, **kwargs)
    #
    # def form_valid(self, form):
    #     trip = form.save(self.request)
    #     return HttpResponseRedirect(self.success_url)

    def get_form_kwargs(self):
        kwargs = super(TripCreationView, self).get_form_kwargs()
        kwargs['user'] = self.request.user
        return kwargs

你知道该怎么办吗?

1 个答案:

答案 0 :(得分:5)

你忘了回复陈述。

def post(self, request, *args, **kwargs):
    self.success_url = request.POST.get('success_url') or reverse('frontend:homepage')
    return super(TripCreationView, self).post(self, request, *args, **kwargs)