【问题标题】:POST multiple files using Django test client in same request在同一请求中使用 Django 测试客户端发布多个文件
【发布时间】:2019-04-25 21:17:55
【问题描述】:

我正在尝试为我的 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 测试客户端发布多个文件或者我应该使用其他东西?

经过修改,更准确

【问题讨论】:

  • Reddit 问题中的一条评论告诉我这不是文件列表,因此我查看了生产代码中的 AJAX 调用,并意识到它正在构建一个集合。我今天将在 python 中尝试一下,看看它是否有效。

标签: python django testing post integration-testing


【解决方案1】:

部分问题在于使用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)

【讨论】:

    【解决方案2】:

    另外,如果你使用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)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-21
      • 2017-02-21
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多