22👍
✅
In the docs, the file field is called attachment
, but in yours, it’s called file
.
You don’t need name
in your post data either — that refers to another field called name
, not the name of the file that you are uploading.
Try the following:
def test_stuff(self):
myfile = open('....\file.csv','r')
response = self.client.post('/', {'file':myfile})
44👍
The way django’s testsuite does it is:
from django.core.files.uploadedfile import SimpleUploadedFile
f = SimpleUploadedFile("file.txt", b"file_content")
This way you don’t need to create a temp file and write to it, and you don’t need to mock a file (not as easy as it sounds).
- [Django]-What is the difference render() and redirect() in Django?
- [Django]-No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model
- [Django]-How to make two django projects share the same database
3👍
Maybe I’m missing something here, but sounds like a job for a good mock library. I personally really like Mock. But, I fall into the camp that believes that your unit tests should be free of all external dependencies (like having to have a file named “file.csv” in a certain location, etc.)
- [Django]-Rendering a value as text instead of field inside a Django Form
- [Django]-Django Admin Form for Many to many relationship
- [Django]-How to force application version on AWS Elastic Beanstalk
Source:stackexchange.com