gpt4 book ai didi

python - 返回多个 HttpResponse 对象,这些对象是 Excel 电子表格

转载 作者:行者123 更新时间:2023-11-30 22:51:01 25 4
gpt4 key购买 nike

期望的结果是导出数据库中所有用户的电子表格,但不是一次性导出所有用户 - 将用户分为 3 组,并将每个组导出为电子表格。

我正在使用 xlsxwriter 和 StringIO

到目前为止,代码仅返回 3 个 HttpResponse 对象中的第一个(也称为第一个用户 block )。我尝试使用 StreamingHttpResponse 但相信我误用了它/它不适合此任务。

问题:我可以在一次回复中返回多个电子表格吗?如果没有,最好的办法是多次调用此函数,每个用户组一次? (在第一条评论后编辑)

非常感谢!

代码如下:

def export_users(request):
# all users
users = User.objects.all()
# breaks up all users into 3 groups
user_groups = [users[x:x+2] for x in xrange(0, len(users), 2)]

# FUNCTIONALITY NOTES --> idea is to split number of users for exporting into groups of 3 then export each group. right now it stops after the first group


# list var for storing all responses
full_response = []

for group in user_groups:
response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=UserReport.xlsx'

# user group
print "group! ---> %s"%(group)

# creates data variable to write to response
xlsx_data = WriteToExcel(group)
response.write(xlsx_data)

# appending each response to an array
full_response.append(response)
print len(full_response)

# all response objects
print full_response

# returning one here
return response

# non-functioning attempt to return all responses

# for response in full_response:
# print response
# return response

最佳答案

您无法返回多个 HttpResponse 对象,但如果将多个文件合并到一个压缩文件中,则可以在一个 HttpResponse 中返回多个文件。

How to return multiple files in HttpResponse Django

关于python - 返回多个 HttpResponse 对象,这些对象是 Excel 电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39133529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com