Labs ICT
⭐ Pro Login

Routes and URL Rules

Mapping URLs to Python functions.

2 min read | Flask Tutorial
⭐

Want the full learning experience?

Get structured courses, certificates, projects, and instructor support with LabsICT Pro.

Explore Pro Courses

Routes and URL Rules

Routing is how Flask maps URLs to your Python functions. The @app.route() decorator is the primary way to define these mappings:

@app.route("/")
def index():
    return "Home page"

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

You can bind multiple URLs to the same function. This is handy when you want the same content accessible from different paths:

@app.route("/home")
@app.route("/")
def index():
    return "Welcome!"

By default, Flask adds a trailing slash automatically. If you visit /about without the slash, it redirects to /about/. You can disable this with strict_slashes=False:

@app.route("/about", strict_slashes=False)
def about():
    return "About"

Flask supports several URL converters for dynamic segments. These let you constrain what values are acceptable:

@app.route("/user/")          # string (default)
@app.route("/post/")       # integer
@app.route("/price/")      # float
@app.route("/filepath/")  # path (allows slashes)
@app.route("/uuid/")         # UUID

If two routes could match the same URL, Flask picks the most specific one. For example, /post/123 matches /post/ before /post/ because the int converter is more specific.

Every route has an endpoint name — by default it's the function name. You can override it if needed:

@app.route("/profile", endpoint="user_profile")
def profile():
    return "Your profile"

Endpoint names matter when you use url_for() to generate URLs dynamically, which is the recommended approach over hardcoding paths.