Search code examples
djangofile-uploadformwizarddjango-formwizard

Using FileField in a FormWizard (Django 1.3)


I am trying to use a Django 1.3 FormWizard to upload a file with 2 steps: 1. Only the FileField 2. If the file was correctly uploaded and valid (after custom validation), offer to give it a name and description.

Following the documentation, I wrote:

class CreateCheckWizard(FormWizard):
    def done(self, request, form_list):
        return HttpResponseRedirect('/my_checks/')
    def get_template(self, step):
        return ['create_check_%s.html' % step, 'create_check_1.html']

class CreateCheckForm1(forms.Form):
    my_file = forms.FileField()

class CreateCheckForm2(forms.Form):
    title = forms.CharField(max_length=255)

I added the multipart/form-data to the FORM tag in the template: <form enctype="multipart/form-data" action="." method="post">

However, even if I upload a file, I get the error "This field is required."

I guess the form is created omitting the request.FILES field. How can we change that behaviour to successfully upload files in the FormWizard?

Edit: Looking at Django source code, it indeed create the forms using form(request.POST) instead of form(request.POST, request.FILES) like it should be to handle files. Any way to upload files without changing the source code?


Solution

  • This isn't possible in the Django 1.3 form wizard. From the Django form wizard docs:

    Important limitation: Because the wizard uses HTML hidden fields to store data between pages, you may not include a FileField in any form except the last one

    It is possible with the Django 1.4 form wizard (see handling files docs). If you're using Django 1.3, you can install the new form wizard as a separate app.