1
0
mirror of https://github.com/django/django.git synced 2025-01-03 06:55:47 +00:00

Refs #31710 -- Improved multiple file upload docs.

This commit is contained in:
Adam Johnson 2024-04-04 23:18:36 +01:00 committed by GitHub
parent e279c724c1
commit ba4ffdc877
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -154,7 +154,7 @@ Uploading multiple files
should be updated after any changes in the following snippets. should be updated after any changes in the following snippets.
If you want to upload multiple files using one form field, create a subclass 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``. to ``True``.
In order for such files to be all validated by your form (and have the value of In order for such files to be all validated by your form (and have the value of
@ -186,14 +186,14 @@ below for an example.
if isinstance(data, (list, tuple)): if isinstance(data, (list, tuple)):
result = [single_file_clean(d, initial) for d in data] result = [single_file_clean(d, initial) for d in data]
else: else:
result = single_file_clean(data, initial) result = [single_file_clean(data, initial)]
return result return result
class FileFieldForm(forms.Form): class FileFieldForm(forms.Form):
file_field = MultipleFileField() 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 :class:`~django.views.generic.edit.FormView` subclass to handle multiple file
uploads: uploads:
@ -209,19 +209,11 @@ uploads:
template_name = "upload.html" # Replace with your template. template_name = "upload.html" # Replace with your template.
success_url = "..." # Replace with your URL or reverse(). 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): def form_valid(self, form):
files = form.cleaned_data["file_field"] files = form.cleaned_data["file_field"]
for f in files: for f in files:
... # Do something with each file. ... # Do something with each file.
return super().form_valid() return super().form_valid(form)
.. warning:: .. warning::