Skip to content

Refs #31710 -- Fixed multiple file upload documentation.#18044

Merged
nessita merged 1 commit intodjango:mainfrom
adamchainz:improve_multiple_file_docs
Apr 4, 2024
Merged

Refs #31710 -- Fixed multiple file upload documentation.#18044
nessita merged 1 commit intodjango:mainfrom
adamchainz:improve_multiple_file_docs

Conversation

@adamchainz
Copy link
Copy Markdown
Member

@adamchainz adamchainz commented Apr 3, 2024

Trac ticket number

ticket-31710

Branch description

Fix two issues in this documentation added in fb4c55d.

First, a small tweak to make the widget consistently return a list of files. Without this, it would break the sample view code’s iteration (for f in files) when the form’s files is not a MultiValueDict, which is typically used in testing.

Before:

In [1]: from django.core.files.uploadedfile import SimpleUploadedFile

In [2]: from example.forms import FileFieldForm

In [3]: form = FileFieldForm({}, {"file_field": SimpleUploadedFile("bunny.jpeg", b"bunny")})

In [4]: form.is_valid()
Out[4]: True

In [5]: form.cleaned_data
Out[5]: {'file_field': <SimpleUploadedFile: bunny.jpeg (text/plain)>}

After:

In [1]: from django.core.files.uploadedfile import SimpleUploadedFile

In [2]: from example.forms import FileFieldForm

In [3]: form = FileFieldForm({}, {"file_field": SimpleUploadedFile("bunny.jpeg", b"bunny")})

In [4]: form.is_valid()
Out[4]: True

In [5]: form.cleaned_data
Out[5]: {'file_field': [<SimpleUploadedFile: bunny.jpeg (text/plain)>]}

Second, remove the redundant post() from the view, and fix the call to super().form_valid() to pass form. The post() method only duplicated ProcessFormView with nothing extra added for multiple file handling. The super() call crashed with TypeError due to the missing argument.

Before:

In [1]: from django.core.files.uploadedfile import SimpleUploadedFile

In [2]: from django.test import RequestFactory

In [3]: from example.views import FileFieldFormView

In [4]: rf = RequestFactory()

In [5]: request = rf.post("/", files={"file_field": SimpleUploadedFile("bunny.jpeg", b"bunny")})

In [6]: FileFieldFormView.as_view()(request)
...
File ~/tmp/multiple-test/example/views.py:22, in FileFieldFormView.form_valid(self, form)
     20 for f in files:
     21     ...  # Do something with each file.
---> 22 return super().form_valid()

TypeError: FormMixin.form_valid() missing 1 required positional argument: 'form'

After:

In [1]: from django.core.files.uploadedfile import SimpleUploadedFile

In [2]: from django.test import RequestFactory

In [3]: from example.views import FileFieldFormView

In [4]: rf = RequestFactory()

In [5]: request = rf.post("/", files={"file_field": SimpleUploadedFile("bunny.jpeg", b"bunny")})

In [6]: FileFieldFormView.as_view()(request)
Out[6]: <HttpResponseRedirect status_code=302, "text/html; charset=utf-8", url="...">

Checklist

  • This PR targets the main branch.
  • The commit message is written in past tense, mentions the ticket number, and ends with a period.
  • I have checked the "Has patch" ticket flag in the Trac system.
  • I have added or updated relevant tests.
  • I have added or updated relevant docs, including release notes if applicable.
  • For UI changes, I have attached screenshots in both light and dark modes.

@adamchainz adamchainz requested review from felixxm, nessita and sarahboyce and removed request for felixxm April 3, 2024 09:55
@adamchainz
Copy link
Copy Markdown
Member Author

Sorry for the post-retirement ping @felixxm 🏖️

@adamchainz adamchainz force-pushed the improve_multiple_file_docs branch 2 times, most recently from 685e222 to e06069e Compare April 3, 2024 10:07
@nessita nessita force-pushed the improve_multiple_file_docs branch from e06069e to d8437c7 Compare April 4, 2024 21:13
Copy link
Copy Markdown
Contributor

@nessita nessita left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I pushed a small edition in the same section, will merge once CI passes. Thank you!

@nessita nessita merged commit ba4ffdc into django:main Apr 4, 2024
@adamchainz adamchainz deleted the improve_multiple_file_docs branch April 22, 2024 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants