如何在 Flask 内部函数中重定向

How to redirect in Flask inside function

我正在 Flask 网站上工作,并且在那

一个视频开始下载,下载完成后,它应该调用一个函数,然后将用户重定向到另一个页面。

.download_complete(function) 会在视频下载成功后运行,这会运行一个带有 args 详细信息和文件路径的函数

def redirectTheUser(details, filepath):
   -Some code that should redirect user-


@app.route("/download")
def download():
    -some code -
    yt.download_complete(redirectTheUser)

我终于知道我能做什么了:

所以我现在做的是重定向但是使用Javascript,如果下载完成变量(使用jinja vars)是真的

Py文件

downloadStatus = False
#By default download status will be False

def redirectUser(self, filepath):
    global downloadStatus
    downloadStatus = True


@app.route("/download")
def download():
    yt.download_complete(redirectUser)
    global downloadStatus
    return render_template("download.html", downloadStatus = downloadStatus)

download.html 文件

<HTML>
........

<body>
....

<script>

downloadComplete = "{{ downloadStatus }}"
if (downloadComplete == "True"){
    window.location.replace("--redirevt your user--")
}

</script>

</body>
</html>