Python 如何在一段时间后将一个网页重定向到另一个网页? @app.route(“/”) def加载(): #返回render_模板('loading.html') 返回“您好,正在加载页面” 时间。睡眠(3) 返回重定向('http://127.0.0.1:5000/home') @应用程序路径(“/home”) def home(): 返回“你好,这是主页”
您好,先生,我想在3秒延迟后将一个网页重定向到另一个网页,以显示第一个网页的内容,然后显示第二个网页的内容。在这个项目中,我使用的是python的Python 如何在一段时间后将一个网页重定向到另一个网页? @app.route(“/”) def加载(): #返回render_模板('loading.html') 返回“您好,正在加载页面” 时间。睡眠(3) 返回重定向('http://127.0.0.1:5000/home') @应用程序路径(“/home”) def home(): 返回“你好,这是主页”,python,html,flask,redirect,Python,Html,Flask,Redirect,您好,先生,我想在3秒延迟后将一个网页重定向到另一个网页,以显示第一个网页的内容,然后显示第二个网页的内容。在这个项目中,我使用的是python的flask库,但我无法做到这一点。我也使用sleep方法,但它不起作用 您能帮我解决这个问题吗?您没有正确使用重定向功能。在烧瓶中,您必须使用: @app.route("/") def loading(): # return render_template('loading.html') return '<h1
flask
库,但我无法做到这一点。我也使用sleep
方法,但它不起作用
您能帮我解决这个问题吗?您没有正确使用重定向功能。在烧瓶中,您必须使用:
@app.route("/")
def loading():
# return render_template('loading.html')
return '<h1> hello it is loading page</h1>'
time.sleep(3)
return redirect('http://127.0.0.1:5000/home')
@app.route('/home')
def home():
return '<h1> hello it is home page</h1>'
确保使用导入的url\u
return redirect(url_for('home'))
此代码将不起作用
from flask import Flask, url_for
再次执行另一个返回(重定向)
您应该在load.html
模板中包含一些javascript
并包括以下脚本:
return redirect('http://127.0.0.1:5000/home')
因此,当您访问root/
时,将显示loading.html页面,3秒钟后,您将被重定向到/home
页面
您可以通过增加或减少上面的setTimeout
函数中的值来增加或减少要重定向的持续时间
<script>
window.setTimeout(function(){
// Move to a new location after 3 secs
window.location.href = "http://127.0.0.1:5000/home";
}, 3000);
</script>
也许你可以使用睡眠功能?是的,先生,我试过了,但不起作用,我试过了,但3秒钟后没有输出,3秒钟后会发生什么我现在注意到你必须在js中这样做。因为你不能在一个功能中有两个返回线路功能。谢谢你,先生。我试过你的代码,它工作正常。我也明白我在哪里犯了错误。
time.sleep(3)
return redirect('http://127.0.0.1:5000/home')
<script>
window.setTimeout(function(){
// Move to a new location after 3 secs
window.location.href = "http://127.0.0.1:5000/home";
}, 3000);
</script>
@app.route("/")
def loading():
return render_template('loading.html')
@app.route('/home')
def home():
return '<h1> hello it is home page</h1>'
<script>
window.setTimeout(function(){
// Move to a new location after 3 secs
window.location.href = "http://127.0.0.1:5000/home";
}, 3000);
</script>
3000 = 3 secs