Django上传文件测试returns301

Django upload file test returns 301

我正在尝试测试允许文件上传的 API post 调用,但我无法让它工作,我总是收到 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 会自动重定向,因为您有 APPEND_SLASH = True

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

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