Python重定向(有延迟)

2024-04-16 19:14:39 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在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')) #with a delay here of 3 secs

该网站工作良好,但没有重定向,我不知道如何拖延整个事情。 思想。。。请:-)


Tags: textappflaskreturndefchecktemplate页面
2条回答

您可以使用return语句并返回字符串格式。在

作为<redirect url>,您可以设置后端可用的任何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>" 

示例:

假设您要提交一个表单,并且有一个运行时问题,需要您在某个时间之后执行此重定向。在

^{pr2}$

好吧,你的函数只返回一个呈现的模板,而不会到达重定向语句。如果要显示页面,然后在延迟后执行重定向,请在模板中使用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中:

^{pr2}$

相关问题 更多 >