【问题标题】:Python redirect (with delay)Python 重定向(有延迟)
【发布时间】:2018-11-15 12:59:17
【问题描述】:

所以我在烧瓶上运行了这个 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

该站点运行良好,但没有重定向,我不知道如何延迟整个过程。 想法...请:-)

【问题讨论】:

  • 使用sleep() 可能会有所帮助。
  • 你不能从一个函数返回两次,也不能告诉浏览器渲染一个页面然后重定向。返回使用 Javascript 重定向的页面或返回 HTTP 重定向。 (你应该明白redirect 方法实际上做了什么)
  • 好的,好吧......两秒钟后页面应该转发到另一个页面。

标签: python redirect flask


【解决方案1】:

好吧,你的函数只是返回一个渲染的模板,永远不会到达重定向语句。如果您想显示一个页面,然后在延迟后进行重定向,请在您的模板中使用 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>

更新:另外,看看 Adrián 提供的另一种(可能更好)方法,您可以在其中返回 Refresh 标头以及呈现的模板响应。

【讨论】:

  • 谢谢。所以我正在尝试这个:@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 但烧瓶崩溃
  • @SabrinaTesla 我已经更新了我的答案,将您的烧瓶视图全部包含在内
  • 谢谢。该网站现在在每种情况下每 2000 毫秒重新加载一次,而不仅仅是 @app.route("/last_visit")
  • 我也收到此错误:BuildError: Could not build url for endpoint 'new_page'。
  • 请仔细阅读烧瓶。快速入门指南是一个很好的起点:flask.pocoo.org/docs/1.0/quickstart
【解决方案2】:

如果您需要渲染模板并在三秒后重定向,您可以将refresh 作为时间传递,并将url 与您需要重定向的 URL 一起传递。

@app.route('/error/', methods=['GET'])
def error():
    return render_template('error.html'), {"Refresh": "1; url=https://google.com"}

【讨论】:

  • 哦,那是一个不错的!谢谢,@Adrián
  • 这比将愚蠢的 setTimeout 放在 javascript 中要好得多
【解决方案3】:

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

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

例子:

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

@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)

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 2014-06-15
    • 2013-02-04
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 2011-04-10
    相关资源
    最近更新 更多