![]() |
VOOZH | about |
A redirect is used to send a user from one URL to another. When a redirect occurs, server responds with an HTTP status code that instructs the browser to load a different location. Redirects are commonly used after form submissions, authentication checks or when a resource has moved.
flask.redirect(location, code=302)
Parameters:
Return: a response object that redirects the user to the specified URL with the given HTTP status code.
HTTP status codes are three-digit numbers returned by the server to indicate how the request should be handled. The following codes are commonly associated with redirects:
| Code | Status |
|---|---|
| 300 | Multiple Choices |
| 301 | Moved Permanently |
| 302 | Found (Temporary Redirect) |
| 303 | See Other |
| 304 | Not Modified |
| 305 | Use Proxy |
| 306 | Reserved |
| 307 | Temporary Redirect |
To perform URL redirection, we need to import the redirect function from the Flask module:
from flask import redirect
The redirect() function returns a response that instructs the browser to navigate to a different URL.
In this example, we create a Flask application that redirects users based on the username entered in a login form.
app.py
Create a templates folder in the project directory and add the following file inside templates/login.html
Run the Flask application using:
python app.py
Output
Case 1: If the Username is admin and the method is POST then it will redirect to the success URL and display logged in successfully.
Case 2: If the username is anything other than admin, the application redirects back to the login page, allowing the user to enter the username again.
url_for() function is used to dynamically generate URLs for a specific function. It takes the function name as the first argument and builds the corresponding URL. This is useful when performing redirects because it avoids hardcoding URLs and makes the application easier to maintain.
Example: Here, the user(name) route checks whether the provided name is admin. If it matches, the request is redirected to the admin page. Otherwise, it redirects to the guest page.
Output: When the URL is /user/admin, the application redirects to the admin page and displays:
👁 ImageWhen the URL is /user/Ayush, the application redirects to the guest page and displays:
👁 ImageThe url_for() function ensures that URLs are generated dynamically, which helps keep routing flexible and prevents issues if route paths change later.
abort() function is used to stop request processing and return an HTTP error response. It is commonly used when a requested resource does not exist, request is invalid or access is not allowed.
abort(code, message=None)
Parameters:
When abort() is called, Flask immediately stops the current request and returns the specified error code to the client.
The following HTTP status codes are commonly used when handling errors in Flask applications:
| Code | Error |
|---|---|
| 400 | Bad request |
| 401 | Unauthenticated |
| 403 | Forbidden |
| 404 | Not Found |
| 406 | Not Acceptable |
| 415 | Unsupported Media Type |
| 429 | Too Many Requests |
Example 1: In this example, we check the username provided in the URL using Error Code 400. If username starts with a number, application raises an error using the abort() function. Otherwise, it returns a success message.
Output
Case 1: If the username does not start with a number, the page displays:
Case 2: If the username starts with a number, Flask raises a 400 Bad Request error.
Example 2: In this example, error code in abort() is changed to 403. If the username starts with a number, the application raises a Forbidden error instead.
Output: If the username starts with a number, Flask returns a 403 Forbidden error. Otherwise, page displays "Good Username".