django/python |从数据库中获取文件,压缩,然后发送到浏览器

django/python |从数据库中获取文件,压缩,然后发送到浏览器,python,django,Python,Django,请帮我解决我的问题 如果这听起来很无聊,我很抱歉,因为我真的很无聊。某个数据库中有文件(jpg、文档),我如何在数据库中选择这些文件,将它们保存在zip文件中,然后作为HttpResponse或类似的内容发送到浏览器: response = HttpResponse(mimetype='application/zip') response['Content-Disposition'] = 'filename=group.zip' 最让我困扰的是从数据库中获取文件的时候,因为谷歌在这方面似乎没有

请帮我解决我的问题

如果这听起来很无聊,我很抱歉,因为我真的很无聊。某个数据库中有文件(jpg、文档),我如何在数据库中选择这些文件,将它们保存在zip文件中,然后作为HttpResponse或类似的内容发送到浏览器:

response = HttpResponse(mimetype='application/zip')
response['Content-Disposition'] = 'filename=group.zip'
最让我困扰的是从数据库中获取文件的时候,因为谷歌在这方面似乎没有什么帮助

我拥有的一些相关代码如下:

temp = request.POST.getlist('cbox') #cbox is the field in form
tempx = models.FileTable.objects.filter(pk__in = temp)

我再也不知道该怎么办了。提前谢谢!:p

您正在寻找和。创建一个
StringIO
,将其作为一个
ZipFile
打开,向其中添加文件,然后。

我找到了这段代码,但我有多种文件类型。files=[]表示thing.objects.all():files.append(“%s.pdf”%(thing.id,),thing.biggish_file())buffer=StringIO()zip=zipfile.zipfile(buffer,“w”,zipfile.zip_DEFLATED)表示名称,f表示文件:zip.writestr(name,f)zip.close()buffer.flush()ret_zip=buffer.getvalue()buffer.close()response.write(ret_zip)return response我认为这是一种简单的方法,但由于我是python新手,我真的不知道如何做到这一点:(