Django SimpleUploadedFile is not being sent

33 views Asked by At

I'm developing Django application and wrote a test that checks if view method acts correctly. I need to send some data in request, including the image file. In my case:

def setUp(self):
   ...
   self.image_file = SimpleUploadedFile("test_image.jpg", b"file_content",
                                         content_type='image/jpeg')
   ...

def test_save_image_instance(self):
   ...
    files = {'image': self.image_file}
    response = self.client.post(endpoint, data=data, files=files)
    # assertions there
   ...

I double-checked, everything is correct, there is SimpleUploadedFile instance in file dictionary.

But in the view request.FILES is empty (<MultiValueDict: {}>), though data was sent correctly. Also, if I look into request.META there is 'files': {'image': <SimpleUploadedFile: test_image.jpg (image/jpeg)>}, which indicates that the file should be there. Django CommonMiddleware is set correctly, type of content in the request is multipart/form-data

I tried using UploadFile class instead of SimpleUploadFile, checking values in different places of the program to find the problem, but it didn't help.

0

There are 0 answers