【发布时间】:2021-09-01 11:57:12
【问题描述】:
我有几个问题,想不通,可能有连接。
问题 1.1: 文件在 django 文档中导出并且它可以工作,但是当我尝试重命名它时,它有一些错误。我想成为这样的with pd.ExcelWriter(newdoc + 'output.xlsx') as writer:,以便每个文件都有一个“新”名称。
我收到了这个错误,TypeError at / unsupported operand type(s) for +: 'InMemoryUploadedFile' and 'str'
问题1.2:如何添加保存路径?
问题 2: 我可以下载文件但它是空的,并且文件的名称是 Donwload.xlsx。我也想变成这样,但是这有很多错误……
filename = newdoc + '_calculated.xlsx'
response = HttpResponse(output, content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = 'attachment; filename=%s' % filename
return response
当我这样做时,我得到了这个......在终端UserWarning: Calling close() 上已经关闭的文件。这在浏览器中
TypeError at / unsupported operand type(s) for +: 'InMemoryUploadedFile' and 'str'
这是views.py,如果代码是这样的,没有错误,但我要下载空文档。
def my_view(request):
if request.method == "POST":
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
output = io.BytesIO()
newdoc = request.FILES['docfile']
dfs = pd.read_excel(newdoc, sheet_name=None, index_col=[0])
with pd.ExcelWriter('output.xlsx') as writer: #problem 1
for name, df in dfs.items():
#pandas code for uploaded excel file
out.to_excel(writer, sheet_name=name)
output.seek(0)
response = HttpResponse(output, content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = 'attachment; filename=%s.xlsx' % 'Download' #problem 2
return response
else:
form = DocumentForm()
return render(request, 'list.html', {'form': form})
【问题讨论】:
标签: python django pandas django-views django-forms