Flask App Routing

Last Updated : 1 Oct, 2026

Flask App Routing is the process of connecting URL paths with Python functions in a Flask application. When a client requests a URL, Flask matches it with a registered route and calls the associated view function. The main routing mechanism is the route() decorator.

  • Create different pages such as home, about and contact.
  • Build dynamic URLs for users, products and posts.
  • Handle different HTTP methods in web applications and APIs.
  • Organize URL mappings as an application grows.

Example:

Python
@app.route("/hello")
def hello():
    return "Hello, Flask!"

Output

Hello, Flask!

Explanation:

  • @app.route("/hello") connects the /hello path with the hello() function.
  • Flask calls hello() when the matching request arrives.
  • Returned string becomes the HTTP response.

Creating Basic Routes

An application can contain multiple routes, with each route connected to its own view function.

Python
from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Welcome to the Home Page"

@app.route("/about")
def about():
    return "About Flask Routing"

@app.route("/contact")
def contact():
    return "Contact Page"

if __name__ == "__main__":
    app.run()

Output

Explanation:

  • The / route is commonly used for the application's home page.
  • Each route has a separate view function.
  • Flask chooses the function according to the requested URL path.
  • Multiple routes can exist in the same Flask application.

Dynamic URLs in Flask

Static routes work when the URL is known beforehand. Dynamic routes are useful when part of the URL changes for different users or resources.

Python
from flask import Flask

app = Flask(__name__)

@app.route("/user/")
def user_profile(username):
    return f"Welcome, {username}"

if __name__ == "__main__":
    app.run()

Output

Screenshot_2026-09-22_08-55-38

Explanation:

  • is a variable part of the URL.
  • Flask passes the value to the view function as a keyword argument.
  • Different usernames can use the same route.
  • A single route can therefore handle many URLs.

Multiple Dynamic Parts in a Route

A route can contain more than one variable section.

Python
from flask import Flask

app = Flask(__name__)

@app.route("/user//post/")
def post(username, post_id):
    return f"User: {username}, Post: {post_id}"

if __name__ == "__main__":
    app.run()

Output

Screenshot_2026-09-22_08-57-22

Explanation:

  • username receives alice.
  • post_id receives 25.
  • Both values are passed to the view function.
  • Dynamic sections can be combined to represent resources such as users, posts, products and orders.

URL Converters in Flask

By default, a variable route part accepts text without a slash. Flask also provides converters to restrict the type of value accepted by a route.

Flask provides these built-in converters:

  • string: Accepts text without a slash. This is the default converter.
  • int: Accepts integer values.
  • float: Accepts floating-point values.
  • path: Accepts text including slashes.
  • any: Matches one value from a predefined set.
  • uuid: Accepts UUID values.
Python
from flask import Flask

app = Flask(__name__)

@app.route("/post/")
def post(post_id):
    return f"Post ID: {post_id}"

if __name__ == "__main__":
    app.run()

Output

The /login route accepts both GET and POST requests.

Explanation:

  • GET can be used to request the page.
  • POST can be used to submit data.
  • A route can support multiple HTTP methods.
  • HEAD and OPTIONS are handled automatically in the relevant cases.

Trailing Slashes

Flask treats trailing slashes as part of the route definition.

Python
@app.route("/projects/")
def projects():
    return "Projects"

Explanation:

  • Route has /projects/ as its canonical form.
  • Accessing /projects redirects to /projects/.
  • But a route defined without a trailing slash generally does not accept the slash-added form automatically.

Building URLs with url_for()

  • Hardcoding URLs throughout an application can make route changes difficult to maintain.
  • Flask provides url_for() to generate a URL from an endpoint name.
Python
from flask import Flask, url_for

app = Flask(__name__)

@app.route("/")
def home():
    return url_for("profile", username="alice")

@app.route("/user/")
def profile(username):
    return f"Profile: {username}"

if __name__ == "__main__":
    app.run()

Output

Screenshot_2026-09-22_09-23-06

Explanation:

  • First argument of url_for() is the endpoint, which normally matches the view function name.
  • Keyword arguments fill dynamic URL variables.
  • URL generation avoids hardcoding paths.
  • Flask also handles escaping and application URL prefixes when constructing URLs.

Mapping URLs with add_url_rule()

Routes can also be registered without using the route() decorator by calling add_url_rule().

The route() decorator is essentially a convenient way to register a view function with Flask's URL routing system.

Python
from flask import Flask

app = Flask(__name__)

def profile(username):
    return f"Profile: {username}"

app.add_url_rule("/user/", "profile", profile)

if __name__ == "__main__":
    app.run()

Output

Screenshot_2026-09-22_09-33-56

Explanation:

  • /user/ defines the URL rule.
  • profile is the endpoint name.
  • profile is the function handling the request.
  • This approach is useful when route registration and function definitions are separated.
Comment