Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python redirect (with delay)

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

like image 576
Sabrina Tesla Avatar asked Nov 15 '18 12:11

Sabrina Tesla


People also ask

How to add time delay in Python?

How to add time delay in Python? In this article, we are going to discuss how do we introduce or add time delay in our program code. In order to add time delay in our program code, we use the sleep () function from the time module. This is the in-built module in Python we don’t need to install externally.

How to use sleep () in Python to delay code?

In order to use sleep (), you need to import it. sleep () takes one argument: seconds. This is the amount of time (in seconds) that you want to delay your code. Now let’s use sleep () in a few different ways.

How to print a string with a 1 second delay in Python?

The code has a for loop that will take the string variable and print each character with a delay of 1 seconds. You can make use of asyncio.sleep with python version 3.4 and higher. To make use of the asyncio sleep method, you need to add async and await to the function, as shown in the example below:

How to make Python wait a few seconds before continuing?

The Python Sleep Function – How to Make Python Wait A Few Seconds Before Continuing, With Example Commands You can use Python’s sleep () function to add a time delay to your code. This function is handy if you want to pause your code between API calls, for example. Or enhance the user’s experience by adding pauses between words or graphics.


2 Answers

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.

like image 137
dmitrybelyakov Avatar answered Sep 22 '22 21:09

dmitrybelyakov


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"}
like image 40
Adrián Avatar answered Sep 21 '22 21:09

Adrián