Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

python - 如何在Flask Python中渲染和重定向到外部URL(How to render_template and then redirect to external URL in Flask Python)

I am trying to redirect a user to a web page with a loading spinner using render_template and then redirect them to an external URL using flask in Python.

(我正在尝试使用render_template使用加载微调器将用户重定向到网页,然后使用Python flask将用户重定向到外部URL。)

I have written the code below, it only waits 3 seconds and then redirects to youtube.com .

(我已在下面编写了代码,它仅等待3秒钟,然后重定向到youtube.com 。)

from flask import Flask, redirect, render_template
from time import sleep

app = Flask(__name__)


@app.route('/')
def redirect_to_url():
    render_template('loading_spinner.html')
    sleep(3)
    return redirect("https://youtube.com")


if __name__ == "__main__":
    app.run(debug=True)

Does anyone have any ideas on how to achieve this?

(有人对如何实现这一目标有任何想法吗?)

  ask by Anatol translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can use a timeout in javascript.

(您可以在javascript中使用超时。)

Here is the sample loding_spinner.html

(这是示例loding_spinner.html)

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
</head>
<body>
Hello
</body>
<script>
   function myFunction() {
   location.replace("https://youtube.com")
   }

   setTimeout(function(){ myFunction(); }, 3000);
</script>
</html>

python Code

(python代码)

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def redirect_to_url():
  return render_template('loading_spinner.html')


if __name__ == "__main__":
  app.run(debug=True)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...