Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将响应与文件下载和重定向结合起来_Javascript_Django_Django Views_Django Templates - Fatal编程技术网

Javascript 将响应与文件下载和重定向结合起来

Javascript 将响应与文件下载和重定向结合起来,javascript,django,django-views,django-templates,Javascript,Django,Django Views,Django Templates,我有一个视图,upn称之为,开始下载: class PrintView(TemplateView): template_name = "proj/listview.html" def get(self, request, *args, **kwargs): try: filepath = TMP.IMG() if os.path.exists(filepath):

我有一个视图,upn称之为,开始下载:

class PrintView(TemplateView): 
    template_name = "proj/listview.html"

    def get(self, request, *args, **kwargs):
        try:
            filepath = TMP.IMG()
            if os.path.exists(filepath):
                with open(filepath, 'rb') as fh:
                    response = HttpResponse(fh.read(), content_type="application/force-download")
                    response['Content-Disposition'] = 'inline; filename=' + os.path.basename(filepath)
                    messages.success(request, "image download started ...")
                    return response
            messages.error(request, "something went wrong!")
            return redirect("index")
        except Exception as e:
            messages.error(request, e)
            return redirect("index")
在我的模板中,我使用下载按钮调用模式窗口,类似于:

<button id="dl_modal_submit" type="submit"</button>
问题是,当我点击我想要的按钮时:

  • 关闭模式
  • 开始图像下载
  • 重定向到索引
  • 有人建议我使用两个视图来实现这一点,但在javascript部分肯定有另一种方法来实现这一点?

    绝对

    script.js:

    $('#button').click(function() {
    
      // close modal:
      $('#modal').hide();
    
      // download files:
      window.open('the_download_url', '_blank');
    
      // relocate:
      window.location = 'the_redirect_url';  
    
    });
    
    $('#button').click(function() {
    
      // close modal:
      $('#modal').hide();
    
      // download files:
      window.open('the_download_url', '_blank');
    
      // relocate:
      window.location = 'the_redirect_url';  
    
    });