包括Django FileUpload的Content-disposition标头 [英] Include Content-disposition header for Django FileUpload

查看:105
本文介绍了包括Django FileUpload的Content-disposition标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个接受文件的API端点(例如,使用Django REST Framework).在Django中,检查响应时可以使用内容处置标头.

I defined an API endpoint which accepts a file (e.g. using Django REST Framework). In Django, the content disposition header can be used when inspecting the response.

现在,如果要在测试端点时设置标头,如何使用REST-Framework的APITestCase包含此标头?

Now, if we want to set the header when testing the endpoint, how do I include this header using REST-Framework's APITestCase?

到目前为止,我尝试过,但是似乎不接受标头.

What I tried so far is, but it does not seem to accept the headers.

class TestSaleViews(APITestCase):
    def test_sale_detail_view(self):
        f = create_named_temporary_file()
        files = {'archive': f}
        basename = os.path.basename(f.name)
        headers = {
            'content-disposition': 'attachment; filename={}'.format(basename),
        }
        response = self.client.post(url, files, format='multipart', **headers)

推荐答案

找到了答案!

Django在其FileUploadParser中对此标头具有固定的关键字.它是: HTTP_CONTENT_DISPOSITION

Django has a fixed keyword for this header in its FileUploadParser. It is: HTTP_CONTENT_DISPOSITION

所以我需要替换它,瞧!有效!

So I needed to replace it et voila: worked!

headers = {
  'HTTP_CONTENT_DISPOSITION': 'attachment; filename={}'.format(basename),
}

https://github.com/编码/django-rest-framework/blob/master/rest_framework/parsers.py#L206

这篇关于包括Django FileUpload的Content-disposition标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆