What is redirection?
Redirection is a common concept in web development that allows the server to redirect a client's request from one URL to another. Redirection is typically used in the following situations:
- URL Change: When the URL of a certain page on a website changes, redirection can be used to redirect requests from the old URL to the new URL.
- Login Verification: When a user attempts to access a page that requires login, if the user is not logged in, they can be redirected to the login page.
- After Form Submission: After a form is submitted, it is common to redirect to another page to avoid resubmitting the form when the user refreshes the page.
- Maintenance or Updates: When a website is undergoing maintenance or updates, users can be redirected to a maintenance page.
In summary: The essence of redirection is jumping.
In the Flask framework, redirection is implemented through the redirect
function, which takes a URL as a parameter and optionally accepts a status code. Typically, redirection is used to navigate users to another page after they complete certain actions. For example, after a user successfully logs in, they can be redirected to their profile page. For instance, some pages cannot be accessed after logging out, so the user can be directly redirected to the homepage after logging out.
Basic usage of redirection
Flask's redirect
function allows you to redirect users from one route to another. By default, redirect
uses a 302 status code, indicating a temporary redirection. If you want to perform a permanent redirection, you can change the status code to 301.
from flask import Flask, redirect
app = Flask(__name__)
@app.route('/')
def index():
# Temporarily redirect to the login page
return redirect('/login')
@app.route('/login')
def login():
return 'Login Page'
if __name__ == '__main__':
app.run(debug=True)
Redirecting and Passing Parameters#
When redirecting, sometimes it is necessary to pass parameters to the target URL. In Flask, you can use the url_for
function to construct a URL with parameters. url_for
takes the endpoint name and a series of keyword arguments, which will be appended to the URL as a query string.
from flask import Flask, redirect, url_for
app = Flask(__name__)
@app.route('/')
def index():
# Pass parameters and redirect to the profile page
return redirect(url_for('profile', name='John'))
@app.route('/profile/<name>')
def profile(name):
# Use the name from the URL parameters
return f'Welcome {name} to the profile page!'
if __name__ == '__main__':
app.run(debug=True)
In the example above, the index
function generates the URL for the profile route with a username parameter using the url_for
function and redirects the user to that URL. The profile
function receives this parameter and uses it to display a personalized welcome message.
Getting Redirect Parameters#
In the target route, you can retrieve the passed parameters using request.args
. This is a dictionary that contains all the passed parameters and their values.
from flask import Flask, request
app = Flask(__name__)
@app.route('/profile')
def profile():
# Get the redirect parameters
username = request.args.get('name')
return f'Welcome {username} to the profile page!'
if __name__ == '__main__':
app.run(debug=True)
In this example, the profile
function uses the request.args.get
method to retrieve the value of the parameter named name
and displays a personalized welcome message.
In summary, redirection in Flask is a powerful feature that not only allows users to navigate from one page to another but also enables the passing of parameters in the process, allowing for more complex navigation logic. Through these techniques, user experience and application interactivity can be effectively enhanced.