English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Flask Redirects and Errors

The Flask class has a redirect() function. When called, it returns a response object and redirects the user to another target location with the specified status code.

The prototype of the redirect() function is as follows -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
Flask.redirect(location, statuscode, response)

In the above function -

location parameters are the URL that the response should be redirected to. statuscode parameters send headers to the browser, defaulting to302. response parameters for instantiating the response.

The following status codes are standardized -

HTTP_300_MULTIPLE_CHOICES HTTP_301_MOVED_PERMANENTLY HTTP_302_FOUND HTTP_303_SEE_OTHER HTTP_304_NOT_MODIFIED HTTP_305_USE_PROXY HTTP_306_RESERVED HTTP_307_TEMPORARY_REDIRECT

The default status code is302This indicates the 'Found' page.

In the following example, the redirect() function is used to re-display the login page again when the login attempt fails.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
from flask import Flask, redirect, url_for, render_template, request
 # Initialize the Flask application
 app = Flask(__name__)
 @app.route('/'))
 def index():
     return render_template('log_in.html')
 @app.route('/login', methods=['POST', 'GET'])
 def login():
     if request.method == 'POST' and
         request.form['username'] == 'admin':
         return redirect(url_for('success'))
     return redirect(url_for('index'))
 @app.route('/success())
 def success():
     return 'logged in successfully'
 if __name__ == '__main__':
     app.run(debug=True)

The Flask class has an abort() function with an error code.

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
Flask.abort(code)

The code parameter uses one of the following values -

400 - For erroneous requests 401 - For unauthenticated 403 - Forbidden 404 - Not found 406 - Not acceptable 415 - For unsupported media types 429 - Too many requests

Here are some minor modifications made to the login() function in the above code. If you want to display the 'Unauthourized' page instead of re-displaying the login page, replace it with an abort(401Call of ).

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by: www.oldtoolbag.com
# Date : 2020-08-08
from flask import Flask, redirect, url_for, render_template, request, abort
 app = Flask(__name__)
 @app.route('/'))
 def index():
    return render_template('log_in.html')
 @app.route('/login', methods=['POST', 'GET'])
 def login():
     if request.method == 'POST':
         if request.form['username'] == 'admin':
             return redirect(url_for('success'))
         else:
             abort(401)
     else:
         return redirect(url_for('index'))
 @app.route('/success())
 def success():
     return 'logged in successfully'
 if __name__ == '__main__':
     app.run(debug=True)