POST 在同一请求中使用 Django 测试客户端的多个文件
POST multiple files using Django test client in same request
我正在尝试为我的 Django 网站上的上传构建一些测试。它允许上传多个文件,所以我需要测试何时上传多个文件。
测试一个文件效果很好:
from django.test import Client
def test_stuff(self):
with open('....\file.csv','rb') as fp:
c = Client()
response = c.post('/', {'name': 'Some Name', 'email': 'some@email.com', 'file': fp})
但是尝试使用文件列表不起作用。
def test_stuff(self):
file_list = # get list of file paths to open
myfiles = []
for file in file_list:
with open('....\file.csv','rb') as fp:
myfiles.append(fp)
c = Client()
response = c.post('/', {'name': 'Some Name', 'email': 'some@email.com', 'file':myfiles})
也没有:
def test_stuff(self):
file_list = # get list of file paths to open
myfiles = []
for file in file_list:
with open('....\file.csv','rb') as fp:
myfiles.append(fp)
c = Client()
response = c.post('/', {'name': 'Some Name', 'email': 'some@email.com',}, files={'file':myfiles})
或
def test_stuff(self):
file_list = # get list of file paths to open
myfiles = []
for file in file_list:
with open('....\file.csv','rb') as fp:
myfiles.append(fp)
c = Client()
response = c.post('/', {'name': 'Some Name', 'email': 'some@email.com'}, files=myfiles)
我的视图从 request.POST.get('myfiles')
获取文件,但 FILES
是空的。
有没有办法使用 Django 测试客户端 POST 多个文件,还是我应该使用其他东西?
Edited to make more accurate
部分问题在于 with
,文件在退出语句时立即关闭。毫不奇怪,另一部分是以正确的格式获取数据。 Django 的测试客户端希望将所有数据作为字典,因此,由于我还发送了用户名和电子邮件,因此需要将其格式化为:
def test_stuff(self):
file_list = # get list of file paths to open
data = {}
files = []
for file in file_list:
fp = open('....\file.csv','rb')
files.append(fp)
data['myfiles'] = files
data['name'] = 'Some Name'
data['email'] = 'some@email.com'
c = Client()
response = c.post('/', data)
此外,如果您使用 SimpleUploadedFile
,您可以传递多个文件:
file1 = SimpleUploadedFile('file1.txt', b'file-1')
file2 = SimpleUploadedFile('file2.txt', b'file-2')
response = self.client.post('some url', data={
'file': [ file1, file2]
})
在视图中它可能是这样的:
class UploadFormView(FormView):
template_name = '...'
form_class = YourForm
# We require to override the post method to manage multiple files.
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
files = request.FILES.getlist('file')
if form.is_valid():
for f in files:
# do smth with file
return self.form_valid(form)
else:
return self.form_invalid(form)
我正在尝试为我的 Django 网站上的上传构建一些测试。它允许上传多个文件,所以我需要测试何时上传多个文件。
测试一个文件效果很好:
from django.test import Client
def test_stuff(self):
with open('....\file.csv','rb') as fp:
c = Client()
response = c.post('/', {'name': 'Some Name', 'email': 'some@email.com', 'file': fp})
但是尝试使用文件列表不起作用。
def test_stuff(self):
file_list = # get list of file paths to open
myfiles = []
for file in file_list:
with open('....\file.csv','rb') as fp:
myfiles.append(fp)
c = Client()
response = c.post('/', {'name': 'Some Name', 'email': 'some@email.com', 'file':myfiles})
也没有:
def test_stuff(self):
file_list = # get list of file paths to open
myfiles = []
for file in file_list:
with open('....\file.csv','rb') as fp:
myfiles.append(fp)
c = Client()
response = c.post('/', {'name': 'Some Name', 'email': 'some@email.com',}, files={'file':myfiles})
或
def test_stuff(self):
file_list = # get list of file paths to open
myfiles = []
for file in file_list:
with open('....\file.csv','rb') as fp:
myfiles.append(fp)
c = Client()
response = c.post('/', {'name': 'Some Name', 'email': 'some@email.com'}, files=myfiles)
我的视图从 request.POST.get('myfiles')
获取文件,但 FILES
是空的。
有没有办法使用 Django 测试客户端 POST 多个文件,还是我应该使用其他东西?
Edited to make more accurate
部分问题在于 with
,文件在退出语句时立即关闭。毫不奇怪,另一部分是以正确的格式获取数据。 Django 的测试客户端希望将所有数据作为字典,因此,由于我还发送了用户名和电子邮件,因此需要将其格式化为:
def test_stuff(self):
file_list = # get list of file paths to open
data = {}
files = []
for file in file_list:
fp = open('....\file.csv','rb')
files.append(fp)
data['myfiles'] = files
data['name'] = 'Some Name'
data['email'] = 'some@email.com'
c = Client()
response = c.post('/', data)
此外,如果您使用 SimpleUploadedFile
,您可以传递多个文件:
file1 = SimpleUploadedFile('file1.txt', b'file-1')
file2 = SimpleUploadedFile('file2.txt', b'file-2')
response = self.client.post('some url', data={
'file': [ file1, file2]
})
在视图中它可能是这样的:
class UploadFormView(FormView):
template_name = '...'
form_class = YourForm
# We require to override the post method to manage multiple files.
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
files = request.FILES.getlist('file')
if form.is_valid():
for f in files:
# do smth with file
return self.form_valid(form)
else:
return self.form_invalid(form)