在 Django 中下载和保存文档的问题
Troubles with downloading and saving a document in django
我有几个问题,想不通,也许有联系。
问题 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})
output = io.BytesIO()
您在这里创建的根本没有被使用。
尝试改变
with pd.ExcelWriter('output.xlsx') as writer:
至
writer = pd.ExcelWriter(output)
否则 BytesIO 可能会被 ExcelWriter 关闭,然后 Django 会尝试再次关闭它。给你双重关闭错误。
您的问题 2 似乎是类型错误。
filename = newdoc + '_calculated.xlsx'
你这里的 newdoc 不是一个字符串,而是一个“InMemoryUploadedFile”,你可能需要通过
访问它的名字
filename = f"{newdoc.name}_calculated.xlsx"
我有几个问题,想不通,也许有联系。
问题 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})
output = io.BytesIO()
您在这里创建的根本没有被使用。
尝试改变
with pd.ExcelWriter('output.xlsx') as writer:
至
writer = pd.ExcelWriter(output)
否则 BytesIO 可能会被 ExcelWriter 关闭,然后 Django 会尝试再次关闭它。给你双重关闭错误。
您的问题 2 似乎是类型错误。
filename = newdoc + '_calculated.xlsx'
你这里的 newdoc 不是一个字符串,而是一个“InMemoryUploadedFile”,你可能需要通过
访问它的名字filename = f"{newdoc.name}_calculated.xlsx"