how to unit test file upload in django

后端 未结 11 1106
感动是毒
感动是毒 2020-12-02 07:09

In my django app, I have a view which accomplishes file upload.The core snippet is like this

...
if  (request.method == \'POST\'):
    if request.FILES.has_         


        
11条回答
  •  醉酒成梦
    2020-12-02 07:50

    if you want to add other data with file upload then follow the below method

    file = open('path/to/file.txt', 'r', encoding='utf-8')

        data = {
            'file_name_to_receive_on_backend': file,
            'param1': 1,
            'param2': 2,
            .
            .
        }
    
        response = self.client.post("/url/to/view", data, format='multipart')`
    

    The only file_name_to_receive_on_backend will be received as a file other params received normally as post paramas.

提交回复
热议问题