I'm writing a test for a Django view, and I want to POST a file. It's a fairly trivial test, and I don't want to litter my tests/ directory with different text files, so I'd like to use an in-memory file and create the content on the fly:
from StringIO import StringIO
file = StringIO('content')
client.post("/", data={'file': file})
Unfortunately this doesn't work:
Traceback (most recent call last):
File "/Users/brad/project/tests/files.py", line 57, in test_set_and_save
'mgmt-current_step': 'Attachments',
File "/Users/brad/django/test/client.py", line 423, in post
response = super(Client, self).post(path, data=data, content_type=content_type, **extra)
File "/Users/brad/django/test/client.py", line 245, in post
post_data = self._encode_data(data, content_type)
File "/Users/brad/django/test/client.py", line 211, in _encode_data
return encode_multipart(BOUNDARY, data)
File "/Users/brad/django/test/client.py", line 117, in encode_multipart
lines.extend(encode_file(boundary, key, value))
File "/Users/brad/django/test/client.py", line 145, in encode_file
content_type = mimetypes.guess_type(file.name)[0]
AttributeError: StringIO instance has no attribute 'name'
Django comes with a set of wrappers for Python's built-in file objects. In this situation django.core.files.base.ContentFile is suitable:
from django.core.files.base import ContentFile
file = ContentFile(b'content', name='plain.txt')
client.post('/', data={'file': file})
ContentFile expects to work with bytes, so don't give it unicode data.
Another trick (if you don't care about the content of the file) is to send the current file:
client.post('/', data={'file': open(__file__, 'rb'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With