【问题标题】:Include Content-disposition header for Django FileUpload包含 Django FileUpload 的 Content-disposition 标头
【发布时间】:2017-10-29 21:13:48
【问题描述】:

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

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

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

到目前为止我尝试的是,但它似乎不接受标题。

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)

【问题讨论】:

    标签: python django unit-testing python-requests content-disposition


    【解决方案1】:

    找到答案了!

    Django 在它的 FileUploadParser 中有一个用于这个头的固定关键字。它是:HTTP_CONTENT_DISPOSITION

    所以我需要更换它,瞧:工作!

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-30
      • 2012-02-27
      • 2011-02-02
      • 1970-01-01
      • 2011-09-27
      • 2018-10-30
      • 2013-09-09
      相关资源
      最近更新 更多