下载Django后重定向或刷新页面

时间:2019-05-10 09:50:42

标签: django redirect zip httpresponse

我有一些代码可以压缩并通过浏览器下载

我尝试参考一些代码,例如使用jquery文件下载使其刷新后刷新

Django: redirect after file download https://jqueryfiledownload.apphb.com/

views.py

def backup(request):
...
...
if request.method == 'POST':
 ...
 ...
 zipper = shutil.make_archive(base_name = 
 os.path.join(settings.MEDIA_ROOT,file_download), format = 'zip', root_dir = backup_dir, base_dir = './' )
 shutil.rmtree(backup_dir)
 resp = HttpResponse(open(zipper, 'rb').read(), content_type = "application/octet-stream")
 resp['Content-Disposition'] = 'attachment; filename=%s.zip' % file_download
 resp['Set-Cookie'] = 'fileDownload=true; Path=/'
 del_dir = os.getcwd()
 os.remove(os.path.join(settings.MEDIA_ROOT,file_download+'.zip'))
 formm.save()
 return resp

可以下载文件

然后我尝试添加js刷新

backup.html


<input id="file_download" type="submit" class="btn btn-primary btn-block" value="Backup" name="backup"/>

<script type="text/javascript">
  $("#file_download").click(function() {
    $.fileDownload($(this).prop('type'), {
      preparingMessageHtml: "The file download will begin shortly, please wait...",
      failMessageHtml: "There was a problem generating your report, please try again."
    });
    return false; //this is critical to stop the click event which will trigger a normal file download!
  });
</script>

在添加结果为js代码后,弹出一个框,显示“生成报告时出现问题,请重试。”


错误日志

[wsgi:error] [pid 8485:tid 139715727099648] [remote 192.168.137.10:34003] Not Found: /submit

1 个答案:

答案 0 :(得分:0)

查看source of the fileDownload plugin会显示Promise.alluser.appId选项。要在成功下载后重新加载页面,请尝试以下操作:

successCallback