[Django] #33757: django.test.Client.post documentation is wrong

6 views
Skip to first unread message

Django

unread,
Jun 1, 2022, 9:45:17 AM6/1/22
to django-...@googlegroups.com
#33757: django.test.Client.post documentation is wrong
------------------------------------------+------------------------
Reporter: bastian-wattro | Owner: nobody
Type: Bug | Status: new
Component: Documentation | Version: 4.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------+------------------------
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
{{{#!python
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)
{{{#!python
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

--
Ticket URL: <https://code.djangoproject.com/ticket/33757>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 1, 2022, 12:59:13 PM6/1/22
to django-...@googlegroups.com
#33757: django.test.Client.post documentation is wrong
--------------------------------+--------------------------------------
Reporter: bastian-wattro | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 4.0
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid


Comment:

Thanks for the report, however it works for me, see also
[https://github.com/django/django/blob/d5bc36203057627f6f7d0c6dc97b31adde6f4313/tests/file_uploads/tests.py#L86-L93
tests].

> What worked was the (way more intuitive) ...

This snippet is exactly the same as the version you think is not working
🤔

If you're having trouble understanding how Django works, see
TicketClosingReasons/UseSupportChannels for ways to get help.

--
Ticket URL: <https://code.djangoproject.com/ticket/33757#comment:1>

Django

unread,
Jun 1, 2022, 1:15:33 PM6/1/22
to django-...@googlegroups.com
#33757: django.test.Client.post documentation is wrong
--------------------------------+--------------------------------------
Reporter: bastian-wattro | Owner: nobody
Type: Bug | Status: closed
Component: Documentation | Version: 4.0
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------

Comment (by Tim Graham):

The example in the documentation initializes a client `c = Client()` while
the snippet here uses `self.client`. It might that the latter version has
some state attached to it (like a login) which makes the difference, but
it's impossible to debug without some minimal example code. Please include
that in future reports (and first confirm a bug in Django using support
channels).

--
Ticket URL: <https://code.djangoproject.com/ticket/33757#comment:2>

Reply all
Reply to author
Forward
0 new messages