Routing, View Functions, URLs, and Flask Request-Response Basics
Learn how Flask maps URLs to Python functions and how web requests become responses.
Inside this chapter
- What Routing Means
- Route Examples
- HTTP Methods
- Returning Responses
- Real Example
Series navigation
Study the chapters in order for the clearest path from Flask basics to scalable application design, APIs, security, and production operations. Use the navigation at the bottom to move smoothly through the full tutorial series.
What Routing Means
Routing connects incoming URLs to specific Python functions, commonly called view functions. This is one of the core ideas in Flask and in web frameworks generally.
Route Examples
@app.route("/about")
def about():
return "About page"
@app.route("/users/<int:user_id>")
def get_user(user_id):
return f"User {user_id}"
Flask supports dynamic URL segments so routes can respond based on path parameters.
HTTP Methods
@app.route("/submit", methods=["GET", "POST"])
def submit():
return "Handled"
Different request methods express different intentions, such as reading, creating, updating, or deleting resources.
Returning Responses
A Flask view can return plain text, rendered HTML, JSON, redirects, or custom response objects. Understanding those response types is key to building both websites and APIs.
Real Example
An educational portal may use routes for home, login, course detail, student dashboard, and admin reports. Each route maps a request into the correct business logic and user-facing response.