Opened 2 years ago

Last modified 2 years ago

#33757 closed Cleanup/optimization

django.test.Client.post documentation is wrong — at Initial Version

Reported by: bastian-wattro Owned by: nobody
Component: Documentation Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Following https://docs.djangoproject.com/en/4.0/topics/testing/tools/#django.test.Client.post
with Django version 4.0.4 to write my first unit tests, I failed to upload a file.

As described there, I tried

with open('mal.csv', 'rb') as fp:
  self.client.post('/post/endpoint/', {'name': 'fred', 'attachment': fp})

And assumed that (as described there)

The name attachment here is not relevant; use whatever name your file-processing code expects.

This made the test fail. Printing request.FILES returned:
<MultiValueDict: {'attachment': [<InMemoryUploadedFile: mal.csv (text/csv)>]}>

What worked was the (way more intuitive)

with open('mal.csv', 'rb') as fp:
  self.client.post('/post/entpoint/', {'name': 'fred', 'attachment': fp})
# <MultiValueDict: {'fred': [<InMemoryUploadedFile: mal.csv (text/csv)>]}>

I did not check:

  • since when the documentation is obsolete
  • if this is intended behavior

Change History (0)

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