【发布时间】:2021-01-27 01:28:58
【问题描述】:
我有一个简单的模型、一个序列化程序和一个视图。我想通过视图上传文件,但没有找到有效的方法。
这是我的代码:
def test_api_post(self):
lesson = self.Create_lesson()
file = SimpleUploadedFile(
"file.txt",
"".join(random.choices(string.ascii_letters + string.digits, k=1024 * 5)).encode(),
"text/plain"
)
response = self.client.post(
"/api/submission/",
{
"lesson": lesson.id,
"file": file
},
format="multipart"
)
self.assertStatusOk(response.status_code) # Error
我尝试使用with open() as file,也尝试使用path.read_bytes()。没有任何效果。
How can I test binary file uploading with django-rest-framework's test client? 不起作用,https://gist.github.com/guillaumepiot/817a70706587da3bd862835c59ef584e 不起作用,how to unit test file upload in django 也不起作用。
【问题讨论】:
-
你得到什么错误?
-
status_code = 400。没有data。content是一些 html 说Bad Request
标签: python django django-rest-framework