Part 7 - Redirects in Flask

October 13th, 2020 3 min read

Most of the time we jump from one page of website to anoother, and so on and so forth, ever wondered how this happens this happens when we are redirects by server or by client side ( such as a tags in html), in this post we are going to see server side part and how to do it in flask, in this post we are going to redirecting users in flask,

We use redirect method from Flask class. Use of redirect method is as follows,

Flask.redirect(location, statuscode, response) where the value of location, statuscode, response are as follows,

  1. location ( Required ) : It denotes the path to which a user is to redirected.
  2. statuscode ( Optional ) : It is an integer value that denotes the list statuscode of the response , by default it is 302 which stands for found.
  3. response : It is used to instantiate response.

Let’s see an example,

In this example we have an list searchEngineList which contains list of famaous search engines and on route checkSearchEngine we will check wether the given the searchengine attribute of the submitted form is in searchEngineList and if it is then it will redirect user to success else user will be redirected to home route from where he has started.

If you are not sure about how to handle forms check out Get and Post requests in Flask - Flask For Noobs 🧪.

from flask import Flask, redirect, url_for, request

app = Flask(__name__)

searchEngineList = ["Bing","Google","Duck Duck Go","Yandex","Youtube"]

@app.route('/')
def home():
	return render_template("home.html")


@app.route('/searchengine', methods = ['POST'])
def checkSearchEngine():
	name = request.form.get("searchengine")

	if name in searchengine:
		return redirect(url_for(success))

	return redirect(url_for(home))

@app.route('/success')
def success():
	return "Yes, it is a famours search engine."

if __name__ == '__main__':
	app.run(debug=True)
	<form method="POST" action="{{ url_for(checkSearchEngine) }}">
		<input type="text" name="searchengine">
		<button type="submit"> Submit </button>
	</form>

As you may have seen in above example that we passed url_for , what is does is it gets url for that spefic route and return it. It is a good practice as even if you change the route the program will not break easily.

Now let’s come to main point about redirect as you may have seen we just passed url, and none other parameters were passed as a result these values will be set to default so statuscode will be 302 , and this is how simple is it to redirect users in within the flask application.

If for some reasons you need to redirect your users to a hard coded url of your application, use the following syntrax,

@app.route('/redirect-to-other-part')
def redirectToAnotherWebsite():
	return redirect("/other/route-of/your-website")

We can also redirect the users out of the flask application to another websites by using the same simple syntrax,

@app.route('/redirect-to-another-website')
def redirectToAnotherWebsite():
	return redirect("https://example.com/")

Here is a list of all the status codes and their meaning, this may help you,

Status Code Standard Meaning
300 MULTIPLE CHOICES
301 MOVED PERMANENTLY
302 FOUND
303 SEE OTHER
304 NOT MODIFIED
305 USE PROXY
306 RESERVED
307 TEMPORARY REDIRECT

Hope you like it 😊.


You may also like: