Flask Routes Basics
A route connects a URL to a Python function. When a user visits a URL in their browser, Flask checks its list of routes, finds the matching one, and runs the associated function. The function's return value becomes what the browser displays.
The Post Office Analogy
Think of routes like a post office sorting system. Every letter (request) arrives with an address (URL). The sorting system (Flask router) reads the address and sends the letter to the right mailbox (view function). The mailbox owner reads it and sends back a reply (response).
Request: GET /contact
│
▼
Flask Router checks routes:
├── '/' → home()
├── '/about' → about()
└── '/contact'→ contact() ← match found!
│
▼
contact() runs and returns HTML
│
▼
Browser displays the contact page
Defining a Basic Route
Every route uses the @app.route() decorator placed directly above the view function:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def home():
return '<h1>Welcome to the Home Page</h1>'
@app.route('/about')
def about():
return '<h1>About Us</h1><p>We build great apps.</p>'
@app.route('/contact')
def contact():
return '<h1>Contact Us</h1>'Each function name must be unique. Flask uses the function name as an internal identifier for the route.
What a View Function Can Return
The simplest return value is a plain string or an HTML string. Flask wraps it in an HTTP 200 response automatically. You can also return a tuple to set a custom status code:
@app.route('/not-here')
def missing():
return '<h1>Page Not Found</h1>', 404The comma separates the response body from the HTTP status code. 404 tells the browser the page was not found.
Mapping Multiple URLs to One Function
One function can respond to multiple URLs. Stack multiple decorators to achieve this:
@app.route('/')
@app.route('/home')
def home():
return '<h1>Home Page</h1>'Both http://127.0.0.1:5000/ and http://127.0.0.1:5000/home now call the same function.
Trailing Slash Behavior
Flask handles trailing slashes in a specific way. Understanding this prevents frustrating 404 errors.
| Route Definition | User visits /about | User visits /about/ |
|---|---|---|
@app.route('/about') | ✓ Works | 404 Not Found |
@app.route('/about/') | Redirects to /about/ | ✓ Works |
When you define a route with a trailing slash like /about/, Flask automatically redirects users who visit /about to /about/. Without the trailing slash in the definition, Flask is strict — only the exact URL matches.
Viewing All Registered Routes
Flask provides a built-in command to list every route your app has registered. Run this in your terminal while inside the project directory:
flask routesThe output looks like this:
Endpoint Methods Rule --------- --------- ---------- about GET /about contact GET /contact home GET / static GET /static/<path:filename>
The static row appears automatically — Flask registers it to serve files from the static folder.
The Endpoint Name
Flask assigns each route an endpoint name. By default, the endpoint name equals the function name. You can set a custom endpoint name using the endpoint parameter:
@app.route('/about', endpoint='about_page')
def about():
return '<h1>About</h1>'Endpoint names become important when you generate URLs programmatically using url_for(), which is covered in a later topic.
How the Flask Router Works Internally
app.py loads
│
▼
Flask reads all @app.route() decorators
│
▼
Builds a URL map:
{ '/': home, '/about': about, '/contact': contact }
│
▼
Server starts and waits
│
Request arrives
│
▼
Flask matches URL against the map
│
▼
Calls matching function → returns response
Summary
Routes are the backbone of every Flask application. Each route pairs a URL pattern with a Python function. When a request arrives, Flask finds the matching route and calls its function. The function produces the response that goes back to the browser. Mastering routes gives you full control over which code runs for every URL in your app.
