CreateView没有返回HttpResponse

时间:2013-05-22 10:43:00

标签: django class-based-views

我有以下视图扩展了基础CreateView:

class PeopleImportCsv(FailedLoginMessageMixin, CreateView):
    model = CsvFile
    form_class = CustomerCsvImportForm
    template_name = 'people/customer_uploadcsv_form.html'

    def get_success_url(self):
        url = reverse('customer_process_csv', args=[self.object.id])
        return url

    def form_valid(self, form):
        instance = form.save(commit=False)
        instance.uploaded_by = self.request.user
        super(PeopleImportCsv, self).form_valid(form)

我正在使用get_success_url()方法,因此我可以在数据库中获取新创建的对象的id。但是,当我尝试提交表单时,我收到以下ValueError消息:

The view people.views.PeopleImportCsv didn't return an HttpResponse object.

如果我在get_success_url()中分配了url后立即放置了assert False,那么我可以看到它有正确的url我正在期待所以我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:5)

您需要return方法中的form_valid(如果您使用的是ModelForm):

def form_valid(self, form):
    instance = form.save(commit=False)
    instance.uploaded_by = self.request.user
    return super(PeopleImportCsv, self).form_valid(form)

你可以see the methods signature in the Django source

P.S 有一个非常有用的网站,可以在这里引用Djangos许多基于类的视图:http://ccbv.co.uk/