how to unit test file upload in django

后端 未结 11 1052
感动是毒
感动是毒 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:56

    I am using Python==3.8.2 , Django==3.0.4, djangorestframework==3.11.0

    I tried self.client.post but got a Resolver404 exception.

    Following worked for me:

    import requests
    upload_url='www.some.com/oaisjdoasjd' # your url to upload
    with open('/home/xyz/video1.webm', 'rb') as video_file:
        # if it was a text file we would perhaps do
        # file = video_file.read()
        response_upload = requests.put(
            upload_url,
            data=video_file,
            headers={'content-type': 'video/webm'}
        )
    

提交回复
热议问题