【问题标题】:Django upload file test returns 301Django上传文件测试返回301
【发布时间】:2017-10-18 03:21:01
【问题描述】:

我正在尝试测试允许文件上传的 API 发布调用,但我无法让它工作,我总是收到 301

with open('video.mp4') as f:
    data = {'file': f}
    response = self.client.post('/api/upload_file', data, format='multipart')

返回的响应是 301

HttpResponsePermanentRedirect status_code=301, "text/html; charset=utf-8", url="/api/v1/assets/upload_file/"

我确保 self.client 已通过身份验证,并且其余测试正常运行

self.client = APIClient()
self.client.force_authenticate(user=self.user)

【问题讨论】:

    标签: django django-rest-framework django-testing django-unittest django-tests


    【解决方案1】:

    您在测试中缺少尾部斜杠,因此 Django 会自动重定向,因为您有 APPEND_SLASH = True

    要解决此问题,请将代码更改为:

    self.client.post('/api/upload_file/', data, format='multipart')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 2010-12-07
      • 1970-01-01
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多