python - 如何在 Flask Python 中渲染模板然后重定向到外部 URL
问题描述
我正在尝试使用 render_template 将用户重定向到带有加载微调器的网页,然后使用flask
Python 将它们重定向到外部 URL。
我已经编写了下面的代码,它只等待 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)
有没有人对如何实现这一目标有任何想法?
解决方案
您可以在 javascript 中使用超时。这是示例 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>
蟒蛇代码
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)
推荐阅读
- python - 每秒刷新一次值 Tkinter
- android - 如何在没有 activity_main.xml 文件的情况下将 admob 广告放置到应用程序
- javascript - 使用 JavaScript 选择 Anchor 标记,该标记使用 PHP 和 Fetch API 在 DOM 中动态呈现
- java - 在Android移动应用创建中,有没有办法知道用户是否在不使用firebase的情况下点击了注册期间发送的验证链接?
- ios - Swift - 视图控制器内部的 CollectionView - 它们之间的数据交换
- c# - HTML5 音频元素在 Internet 信息服务服务器中不起作用
- python - pyautogui 没有将屏幕截图保存到我告诉它的目录
- django - Django模板中的嵌套字典
- php - 如何dockerize一个微服务php应用程序?
- javascript - Javascript CSS 星级评分系统悬停