Changes between Initial Version and Version 3 of Ticket #33757
- Timestamp:
- Jun 2, 2022, 4:32:21 AM (2 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #33757
- Property Summary django.test.Client.post documentation is wrong → django.test.Client.post documentation is confusing
-
Ticket #33757 – Description
initial v3 1 1 Following [https://docs.djangoproject.com/en/4.0/topics/testing/tools/#django.test.Client.post] 2 with Django version 4.0.4 to write my first unit tests, I failed to upload a file. 3 4 As described there, I tried 5 {{{#!python 2 with Django version 4.0.4 to write my first unit tests, I failed to upload a file under a given keyword *with the *name* / *attachment* pattern* 3 4 As described there, I tried to set *name* and *file pointer* like this: 5 > Submitting files is a special case. To POST a file, you need only provide the file field name as a key, and a file handle to the file you wish to upload as a value. For example: 6 > {{{#!python 6 7 with open('mal.csv', 'rb') as fp: 7 8 self.client.post('/post/endpoint/', {'name': 'fred', 'attachment': fp}) 8 9 }}} 9 And assumed that (as described there) 10 > The name attachment here is not relevant; use whatever name your file-processing code expects. 10 > The name *attachment* here is not relevant; use whatever name your file-processing code expects. 11 11 12 This made the test fail. Printing request.FILES returned: 13 `<MultiValueDict: {'attachment': [<InMemoryUploadedFile: mal.csv (text/csv)>]}>` 12 This made me assume that the file given via the filepointer will be accessible via the keyword `fred`. 14 13 15 What worked was the (way more intuitive) 14 This [test](https://github.com/django/django/blob/d5bc36203057627f6f7d0c6dc97b31adde6f4313/tests/file_uploads/tests.py#L86-L93) works because the [endpoint being called](https://github.com/django/django/blob/d5bc36203057627f6f7d0c6dc97b31adde6f4313/tests/file_uploads/views.py#L18) (contrary to what I would expect) checks the keyword of the file pointer (`fiel_field` in this case) and the `name` separately. 15 16 What I wanted was 16 17 {{{#!python 17 18 with open('mal.csv', 'rb') as fp: 18 self.client.post('/post/entpoint/', {'name': 'fred', 'attachment': fp}) 19 # <MultiValueDict: {'fred': [<InMemoryUploadedFile: mal.csv (text/csv)>]}> 19 self.client.post('/post/endpoint/', {'fred': fp}) 20 20 }}} 21 as (contraty to what is written in the docs) **name** is not required 21 22 22 I did not check: 23 * since when the documentation is obsolete 24 * if this is intended behavior 23 24 Sorry for the confusion and thanks for the quick responses.