包括Django FileUpload的Content

编程入门 行业动态 更新时间:2024-10-11 01:10:28
本文介绍了包括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.

docs.djangoproject/en/1.11/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment

现在,如果要在测试端点时设置标头,如何使用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), }

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

更多推荐

包括Django FileUpload的Content

本文发布于:2023-10-10 09:29:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478300.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Django   FileUpload   Content

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!