下移文件后重定向到主页-Flask

时间:2018-10-12 22:28:29

标签: python redirect flask url-for

有这样的脚本,可以让用户下载媒体文件。脚本在下载后删除

@app.route('/done/<path:filename>', methods=['GET', 'POST'])
def indir(filename):
    path = os.getcwd()+"\\"+"file.mp4"
    def generate():
        try:
            f=open(path,"rb")
            yield from f
        finally:
            f.close()
            os.remove(path)



    r = app.response_class(generate(), mimetype='video/mp4')
    r.headers.set('Content-Disposition', 'attachment', filename=filename)
    return r

但是,下载完成后,我无法重定向到主页

我在finally语句的结尾尝试过

return redirect(url_for('home')) 

给出此错误

Flask.url_for() error: Attempted to generate a URL without the application context being pushed

并尝试

return render_template('home.html')

给出该错误

Debugging middleware caught exception in streamed response at a point where response headers were already sent.AttributeError: 'NoneType' object has no attribute 'app'

我几个小时都找不到合适的解决方案

感谢您的帮助

0 个答案:

没有答案