Changes between Initial Version and Version 3 of Ticket #33757


Ignore:
Timestamp:
Jun 2, 2022, 4:32:21 AM (4 years ago)
Author:
bastian-wattro
Comment:

Sorry for the confusion and thanks for the quick responses. I'll try to clarify:

Following ​the docs 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

The docs state:

>>> c = Client()
>>> with open('wishlist.doc', 'rb') as fp:
...     c.post('/customers/wishes/', {'name': 'fred', 'attachment': fp})


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

This made me assume that the file given via the file pointer will be accessible via the keyword fred.

This ​test works because the ​endpoint being called (contrary to what I would expect) checks the keyword of the file pointer (fiel_field in this case) and the name separately.

Reading the docs for a third time now I get the meaning.

What I don't understand is why the keyword 'name' is there, and why doesn't it simply say something like:


To submit a file, provide a file handle to the file you wish to upload as a value. For example:

>>> c = Client()
>>> with open('wishlist.doc', 'rb') as fp:
...     c.post('/customers/wishes/', {'wishes_file': fp})

will be accessible via request.FILES['wishes_file']


Again thanks for your time. I get now that this is something that probably only confuses new comers like my self.

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  
    11Following [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
     2with 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 
     4As 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
    67with open('mal.csv', 'rb') as fp:
    78  self.client.post('/post/endpoint/', {'name': 'fred', 'attachment': fp})
    89}}}
    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.
    1111
    12 This made the test fail. Printing request.FILES returned:
    13 `<MultiValueDict: {'attachment': [<InMemoryUploadedFile: mal.csv (text/csv)>]}>`
     12This made me assume that the file given via the filepointer will be accessible via the keyword `fred`.
    1413
    15 What worked was the (way more intuitive)
     14This [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
     16What I wanted was
    1617{{{#!python
    1718with 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})
    2020}}}
     21as (contraty to what is written in the docs) **name** is not required
    2122
    22 I did not check:
    23 *  since when the documentation is obsolete
    24 * if this is intended behavior
     23
     24Sorry for the confusion and thanks for the quick responses.
Back to Top