Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CreateView is not returning an HttpResponse

I have the following view which extends the base 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)

I am using the get_success_url() method so I can get the id of the newly created object in the database. However, when I attempt to submit my form I get the following ValueError message:

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

If I place an assert False immediately after assigning the url in get_success_url() then I can see that it has the correct url I'm expecting so what can I do to sort this out?

like image 377
hellsgate Avatar asked Mar 23 '23 21:03

hellsgate


1 Answers

You need to have a return from the form_valid method (if you are using a ModelForm):

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

You can see the methods signature in the Django source

P.S There is a very useful site for referencing Djangos many class based views here: http://ccbv.co.uk/

like image 162
Timmy O'Mahony Avatar answered Mar 31 '23 15:03

Timmy O'Mahony