我正在从查询数据库生成一个csv文件,并希望将该文件返回给用户下载。该文件已损坏,就像下载在完成之前已中断一样。使用的代码如下:
class Echo(object):
def write(self, value):
return value
pseudo_buffer = Echo()
csv_data = []
for d in pesquisa:
csv_data.append([d[h] for h in data])
writer = csv.writer(pseudo_buffer)
response = StreamingHttpResponse((writer.writerow(row) for row in csv_data),
content_type="text/csv")
response['Content-Disposition'] = 'attachment; filename="report.csv"'
return response
我正在使用Django 1.8.9 + Gunicorn + Nginx
如果我使用django测试服务器,则会出现以下错误:
ConnectionResetError: [Errno 104] Connection reset by peer
答案 0 :(得分:0)
无需StreamingHttpResponse
:
csv_data = []
for d in pesquisa:
csv_data.append([d[h] for h in data])
response = HttpResponse(content_type='text/csv')
writer = csv.writer(response)
writer.writerows(csv_data)
response['Content-Disposition'] = 'attachment; filename="report.csv"'
return response