Routing and URL Handling - benhall/flask-demo

Routing and URL Handling in Flask

In Flask, routing refers to the process of associating URLs with specific view functions. This is done using the @app.route() decorator. The argument to this decorator is the URL pattern that the view function will respond to.

For example, the following code defines two routes:

@app.route("/")
def home():
return "Welcome to the HomePage!"

@app.route("/educative")
def learn():
return "Happy Learning at Educative!"

The first route, "/", is associated with the home() function, and the second route, "/educative", is associated with the learn() function. When a user navigates to http://localhost:3000/ in their web browser, the home() function will be executed and its return value will be displayed. Similarly, navigating to http://localhost:3000/educative will execute the learn() function.

Static Routing

In static routing, the URL pattern is a constant string. This is the simplest and most common type of routing in Flask.

Here is an example of a Flask application that uses static routing:

from flask import Flask, render_template
app = Flask(__name__)

@app.route("/")
def home():
return "Welcome to the HomePage!"

@app.route("/educative")
def learn():
return "Happy Learning at Educative!"

if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=3000)

In this example, the home() function is associated with the "/" route, and the learn() function is associated with the "/educative" route.

Dynamic Routing

In dynamic routing, the URL pattern includes one or more variables. These variables are surrounded by < and > in the URL pattern, and their values can be accessed in the view function using the same name as the variable.

Here is an example of a Flask application that uses dynamic routing:

@app.route("/user/<username>")
def show_user(username):
return "User %s" % username

In this example, the show_user() function is associated with the "/user/<username>" route. The value of the username variable can be accessed in the show_user() function.

URL Building

Flask provides the url_for() function for building URLs in a way that is independent of the current application context. This function takes the name of the view function as its first argument, and any number of keyword arguments that correspond to the variable names in the URL pattern.

Here is an example of using url_for() to build a URL:

@app.route("/user/<username>")
def show_user(username):
user_url = url_for('show_user', username=username)
return "User %s can be found at %s" % (username, user_url)

In this example, the url_for() function is used to build the URL for the show_user() function, using the username variable as an argument.

Redirection

Flask provides the redirect() function for redirecting the user to a different URL. This function takes a single argument, which is the URL to redirect to.

Here is an example of using redirect() to redirect the user:

@app.route("/old-page")
def redirect_page():
return redirect("/new-page")

In this example, the redirect_page() function is associated with the "/old-page" route. When this route is accessed, the user will be redirected to the "/new-page" route.

Trailing Slashes

By default, Flask will redirect requests with a trailing slash to the same URL without the trailing slash. This behavior can be changed by setting the APPEND_SLASH configuration option to False.

Here is an example of disabling trailing slash redirection:

app = Flask(__name__)
app.config['APPEND_SLASH'] = False

@app.route("/page")
def show_page():
return "This is the page!"

In this example, the APPEND_SLASH configuration option is set to False, which disables trailing slash redirection. When the "/page" route is accessed, the user will not be redirected to "/page/".

Sources