Python重定向(带延迟)
我在flask上运行了这个python页面。它工作正常,直到我想要重定向Python重定向(带延迟),python,redirect,flask,Python,Redirect,Flask,我在flask上运行了这个python页面。它工作正常,直到我想要重定向 @app.route("/last_visit") def check_last_watered(): templateData = template(text = water.get_last_watered()) return render_template('main.html', **templateData) return redirect(url_for('new_page')) #wi
@app.route("/last_visit")
def check_last_watered():
templateData = template(text = water.get_last_watered())
return render_template('main.html', **templateData)
return redirect(url_for('new_page')) #with a delay here of 3 secs
该网站运作良好,但没有重定向,我不知道如何推迟整个事情。
思想。。。请:-)好吧,您的函数只返回一个呈现的模板,永远不会到达重定向语句。如果要显示页面,然后在延迟后执行重定向,请在模板中使用javascript重定向:
@app.route("/last_visit")
def check_last_watered():
templateData = template(text = water.get_last_watered())
templateData['redirect_url'] = url_for('new_page')
return render_template('main.html', **templateData)
然后在main.html
中:
<script>
setTimeout(function(){
window.location.href = '{{redirect_url}}';
}, 2000);
</script>
setTimeout(函数(){
window.location.href='{{redirect_url}}}';
}, 2000);
您可以使用return
语句并返回字符串格式
正如
一样,您可以设置从可用到后端的任何URL
此解决方案与dmitrybelyakov的类似,但它是一个快速修复方案,无需处理新的小型html模板
return f"<html><body><p>You will be redirected in 3 seconds</p><script>var timer = setTimeout(function() {{window.location='{ <redirect url> }'}}, 3000);</script></body></html>"
return f“您将在3秒钟内被重定向var timer=setTimeout(function(){{window.location={},3000);”
例如:
假设您想要提交一个表单,并且有一个运行时问题,要求您在特定时间后执行此重定向
@app.route('/')
def your_func():
form = FormFunc()
if form.validate_on_submit():
# do something
wait_time = 3000
seconds = wait_time / 1000
redirect_url = 'https://www.example.com/'
# if the validation is successful, forward to redirect page, wait 3 seconds, redirect to specified url
return f"<html><body><p>You will be redirected in { seconds } seconds</p><script>var timer = setTimeout(function() {{window.location='{ redirect url }'}}, { wait_time });</script></body></html>"
# if the validation is not successful, reload the form
return render_template('formPage.html', form=form)
@app.route(“/”)
定义您的函数():
form=FormFunc()
if form.validate_on_submit():
#做点什么
等待时间=3000
秒=等待时间/1000
重定向https://www.example.com/'
#如果验证成功,请转发到重定向页面,等待3秒钟,重定向到指定的url
返回f“您将在{seconds}秒内被重定向var timer=setTimeout(函数(){{window.location={redirect url}}}},{wait_time}”)
#如果验证未成功,请重新加载表单
返回呈现模板('formPage.html',form=form)
如果需要渲染模板并在三秒后重定向,则可以将刷新
作为时间和url
与需要重定向的url一起传递
@app.route('/error/', methods=['GET'])
def error():
return render_template('error.html'), {"Refresh": "1; url=https://google.com"}
使用
sleep()
可能会有所帮助。您不能从函数返回两次,也不能告诉浏览器呈现页面然后重定向。返回使用Javascript重定向的页面或返回HTTP重定向。(您可能应该了解重定向
方法的实际功能)好吧,好吧。。。两秒钟后,页面应转到另一页。谢谢。所以我尝试以下方法:@app.route(“/last_visit”)def check_last_watered():templateData=template(text=water.get_last_watered())return render_template('main.html',**templateData['main.html']=url_for('new page'))在main.html中使用javascript,但是crashes@SabrinaTesla我已经更新了我的答案,将您的flask视图包含在其完整的Tank you中。该网站现在每2000毫秒都会重新加载一次,不仅仅是@app.route(“/last_visit”),我还遇到了以下错误:BuildError:无法为端点“new_page”构建url。请仔细阅读。快速入门指南是一个很好的起点: