#34143 closed Uncategorized (invalid)

Multiple file upload docs

Reported by: Guillaume LEBRETON Owned by: Guillaume LEBRETON
Component: Documentation Version: 4.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Guillaume LEBRETON)

In the docs, https://docs.djangoproject.com/en/dev/topics/http/file-uploads/#uploading-multiple-files
there is this code snippet:

def post(self, request, *args, **kwargs):
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        files = request.FILES.getlist('file_field')
        if form.is_valid():
            for f in files:
                ...  # Do something with each file.
            return self.form_valid(form)
        else:
            return self.form_invalid(form)

I think it's a bit confusing to use form validation and then get the file list directly from the request.
I found that replacing files = request.FILES.getlist('file_field') by files = form.files.getlist('file_field') seems to be more consistent with the usual django form approach.

The best approach would be to access file_field from cleaned_data but it's not really possible with multple files for now.

PR link: https://github.com/django/django/pull/16265

Change History (3)

comment:1 by Guillaume LEBRETON, 18 months ago

Has patch: set
Owner: changed from nobody to Guillaume LEBRETON
Status: newassigned

comment:2 by Guillaume LEBRETON, 18 months ago

Description: modified (diff)

comment:3 by Mariusz Felisiak, 18 months ago

Component: UncategorizedDocumentation
Resolution: invalid
Status: assignedclosed

Thanks for this patch, however "​Uploading multiple files" contains only a simple example how you can handle multiple files (see related #31710). Moreover Form.files is an undocumented API.

Note: See TracTickets for help on using tickets.
Back to Top