6

So I have this python page running on flask. It works fine until I want to have a redirect.

@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

The site works fine, but does not redirect and I wouldn't know how to delay the whole thing. Ideas... Please :-)

Sabrina Tesla
  • 87
  • 1
  • 4

3 Answers3

5

Well, your function just returns a rendered template and never reaches the redirect statement. If you want to show a page AND then do a redirect after a delay, use a javascript redirect in your template:

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

Then in your main.html:

<script>
    setTimeout(function(){
        window.location.href = '{{redirect_url}}';
    }, 2000);
</script>

UPDATE: Also, have a look at an alternative (and possibly better) way by Adrián, where you can return a Refresh header along with your rendered template response.

dmitrybelyakov
  • 3,709
  • 2
  • 22
  • 26
  • Thanks. So I am trying this: @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')) with the javascript in the main.html but flask crashes – Sabrina Tesla Nov 15 '18 at 13:17
  • @SabrinaTesla I have updated my answer to include your flask view in its entirety – dmitrybelyakov Nov 15 '18 at 13:23
  • Thank you. the site is now reloading every 2000ms in every case, not just for @app.route("/last_visit") – Sabrina Tesla Nov 15 '18 at 15:15
  • I am also getting this error: BuildError: Could not build url for endpoint 'new_page'. – Sabrina Tesla Nov 15 '18 at 15:19
  • 1
    Please read up on flask a little. A good point to start is the quickstart guide: http://flask.pocoo.org/docs/1.0/quickstart/ – dmitrybelyakov Nov 15 '18 at 15:58
5

If you need to render a template and redirect after three seconds you can pass refresh as time and url with the URL you need to redirect.

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

You could use a return statement and return a string format.

As the <redirect url> you could set any URL from available to your back-end.

This solution is similar to dmitrybelyakov's, but it's a quick fix without having to deal with new, small html templates.

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>" 

Example:

Lets say you want to submit a form and have a run time issue that requires you to do this redirect after a certain time.

@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)
blkpingu
  • 1,556
  • 1
  • 18
  • 41