From ba4ffdc8771c2f38cf6de26a2b82bbceea2b933a Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Thu, 4 Apr 2024 23:18:36 +0100 Subject: [PATCH] Refs #31710 -- Improved multiple file upload docs. --- docs/topics/http/file-uploads.txt | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/docs/topics/http/file-uploads.txt b/docs/topics/http/file-uploads.txt index 911c282157..b9a9bbf416 100644 --- a/docs/topics/http/file-uploads.txt +++ b/docs/topics/http/file-uploads.txt @@ -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 @@ -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: @@ -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::