Python django:触发下载后重定向到另一个页面
我想用户被重定向到另一个页面后,他的文件下载被触发。我该怎么做?下面是我关于如何触发下载的代码。或者,如何将在第一个网页中创建的数据帧解析为用户重定向到的网页,然后触发下载?感谢您的帮助。谢谢大家!Python django:触发下载后重定向到另一个页面,python,django,Python,Django,我想用户被重定向到另一个页面后,他的文件下载被触发。我该怎么做?下面是我关于如何触发下载的代码。或者,如何将在第一个网页中创建的数据帧解析为用户重定向到的网页,然后触发下载?感谢您的帮助。谢谢大家! 从django.exe导入渲染 从django.http导入HttpResponse 从。表格导入* from.functions导入* 从…起导入模型 def索引(请求): 如果request.method==“POST”: #上传文件 file=request.FILES['excelfile'
从django.exe导入渲染
从django.http导入HttpResponse
从。表格导入*
from.functions导入*
从…起导入模型
def索引(请求):
如果request.method==“POST”:
#上传文件
file=request.FILES['excelfile']
df=createDF(文件)
#写入xlsx并触发下载
response=HttpResponse(content_type='application/vnd.openxmlformats of icedocument.spreadsheetml.sheet')
响应['Content-Disposition']='附件;filename=“somefile.xlsx”)
df.to_excel(响应,索引=False)
返回响应
#用于上传的呈现表单
返回呈现(请求'webpage/index.html')
您可以为您提到的数据帧创建一个视图,在URL.py文件中注册该视图,并且您可以在发生某些事情后在重定向方法中使用此新视图,例如使用后单击下载按钮重定向到带有数据帧的新页面并开始下载,或者可能在上一个视图中已开始下载
def dataframe(request):
# code to generate your dataframe, not sure how it works with your data.
return render(request, 'your_dataframe_template.html')
内部URL.py
urlpatterns = [
path('dataframeurl/', views.dataframe, name='dataframecoolname') # use some better name in the name argument
]
现在,您可以在带有重定向方法的视图中使用name
参数
# the redirect would be something like this
def download_view(request):
# some logic you want, then...
return redirect(dataframecoolname) # must be the same name argument you used in the url
你可以使用重定向来实现它!