Flask 渲染页面然后重定向到外部站点?

Flask to render a page and then redirect to an external site?

我正在尝试在 python 脚本在后台成功后通过 Flask 重定向到外部站点(例如:google.com)。我不确定如何同时重定向和呈现模板。

这是烧瓶代码:


@app.route ("/result",methods=['POST','GET'])  
def result():
    if request.method == "POST":
        ip1=request.form.get('ip')
        output=subprocess.getstatusoutput(["ping -c 2 " +str(ip1)])
        out=output[1]
        ec=output[0]
        return render_template("home.html",out=output[1])
        sleep(3)                                            // Time given so that the output of ping command above gets printed in html page 
        return redirect ("http://www.google.com/",code=307) // Sample External site
 

The above code works till rendering the page "home.html" and prints the output from "ping" command , but doesn't redirect upon successful execution . Is there a way where we can get the redirect based on the exit code of a command's success or show a failure message on error exit code       

我相信当您 return 第 (-3) 行的模板时,您的方法就完成了。

在我看来,您想向客户端发送重定向 header,但您已经发送了一些内容。你做不到。

我的想法:保留 return render_template 部分,稍后通过 javascript (check here)

执行重定向