【问题标题】:Allow user to download file and then redirect to other page允许用户下载文件然后重定向到其他页面
【发布时间】:2021-03-15 20:34:11
【问题描述】:

我有一个类似的功能:

def download(requests):
    with open(file_path2, 'rb') as fh:
         response = HttpResponse(fh.read(), content_type="application/vnd.ms-excel")
         response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path2)
         return response    #it stop executing here as we return.
    os.remove(file_path2) 
    return render(request, 'user/charts.html', {'res': 'The file has decrypted'})

所以该函数需要做的是,开始下载文件,下载后从系统中删除文件,然后重定向到其他页面。

但问题是,为了实现下载功能,我使用return然后它停止了,那么我该如何执行下一条语句?

【问题讨论】:

    标签: python-3.x django file download


    【解决方案1】:

    您可以打印文件并写入continue,而不是return

    我建议做的是通过将download 属性添加到 HTML 标记来允许文件可下载:

    <a href="image.jpg" download>
    

    现在对于重定向,您可以编写一个函数,一旦单击此按钮即可将redirect 用户调用到另一个页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-31
      • 2020-01-29
      相关资源
      最近更新 更多