Open In App

Redirecting to URL in Flask

Improve
Improve
Like Article
Like
Save
Share
Report

Flask is a backend server that is built entirely using Python. It is a  framework that consists of Python packages and modules. It is lightweight which makes developing backend applications quicker with its features. In this article, we will learn to redirect a URL in the Flask web application.

Redirect to a URL in Flask

A redirect is used in the Flask class to send the user to a particular URL with the status code. conversely, this status code additionally identifies the issue. When we access a website, our browser sends a request to the server, and the server replies with what is known as the HTTP status code, which is a three-digit number.

Syntax of Redirect in Flask

Syntax: flask.redirect(location,code=302)

Parameters:

  • location(str): the location which URL directs to.
  • code(int): The status code for Redirect.
  • Code: The default code is 302 which means that the move is only temporary.

Return: The response object and redirects the user to another target location with the specified code.

The different  types of HTTP codes are:

Code

Status

300 Multiple_choices
301 Moved_permanently
302 Found
303 See_other
304 Not_modified
305 Use_proxy
306 Reserved
307 Temporary_redirect

How To Redirect To Url in Flask

In this example, we have created a sample flask application app.py. It has two routes, One is the base route \ and another is \helloworld route. Here when a user hits the base URL \ (root) it will redirect to \helloworld.

app.py

Python3




# import flast module
from flask import Flask, redirect
 
# instance of flask application
app = Flask(__name__)
 
# home route that redirects to
# helloworld page
@app.route("/")
def home():
    return redirect("/helloworld")
 
# route that returns hello world text
@app.route("/helloworld")
def hello_world():
    return "<p>Hello, World from \
                redirected page.!</p>"
 
 
if __name__ == '__main__':
    app.run(debug=True)


Output:

We hit the base URL in the browser as shown below. As soon we hit the URL flask application returns the redirect function and redirects to the given URL.

 

 

url_for() Function in Flask

Another method you can use when performing redirects in Flask is the url_for() function URL building. This function accepts the name of the function as the first argument, and the function named user(name) accepts the value through the input URL. It checks whether the received argument matches the ‘admin’ argument or not. If it matches, then the function hello_admin() is called else the hello_guest() is called.

Python3




from flask import Flask, redirect, url_for
 
app = Flask(__name__)
 
# decorator for route(argument) function
@app.route('/admin')
# binding to hello_admin call
def hello_admin():
    return 'Hello Admin'
 
@app.route('/guest/<guest>')
# binding to hello_guest call
def hello_guest(guest):
    return 'Hello %s as Guest' % guest
 
@app.route('/user/<name>')
def hello_user(name):
 
    # dynamic binding of URL to function
    if name == 'admin':
        return redirect(url_for('hello_admin'))
    else:
        return redirect(url_for('hello_guest'
                                , guest=name))
 
if __name__ == '__main__':
    app.run(debug=True)


Output:

hello_guest function

hello_user function

Related Article:  Python Flask – Redirect and Errors



Last Updated : 21 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads