Skip to content

Commit

Permalink
Refs #31710 -- Improved multiple file upload docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz authored Apr 4, 2024
1 parent e279c72 commit ba4ffdc
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions docs/topics/http/file-uploads.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Uploading multiple files
should be updated after any changes in the following snippets.

If you want to upload multiple files using one form field, create a subclass
of the field's widget and set the ``allow_multiple_selected`` attribute on it
of the field's widget and set its ``allow_multiple_selected`` class attribute
to ``True``.

In order for such files to be all validated by your form (and have the value of
Expand Down Expand Up @@ -186,14 +186,14 @@ below for an example.
if isinstance(data, (list, tuple)):
result = [single_file_clean(d, initial) for d in data]
else:
result = single_file_clean(data, initial)
result = [single_file_clean(data, initial)]
return result


class FileFieldForm(forms.Form):
file_field = MultipleFileField()

Then override the ``post`` method of your
Then override the ``form_valid()`` method of your
:class:`~django.views.generic.edit.FormView` subclass to handle multiple file
uploads:

Expand All @@ -209,19 +209,11 @@ uploads:
template_name = "upload.html" # Replace with your template.
success_url = "..." # Replace with your URL or reverse().

def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)

def form_valid(self, form):
files = form.cleaned_data["file_field"]
for f in files:
... # Do something with each file.
return super().form_valid()
return super().form_valid(form)

.. warning::

Expand Down

0 comments on commit ba4ffdc

Please sign in to comment.