Uploaded: Nov. 29 2025 | Modified: Nov. 29 2025
Flask Routing Explained: Static vs Dynamic Routes (Beginner-Friendly Guide)
Routing is one of the most important concepts in Flask. It defines how your application responds to specific URLs. Whether you're building simple pages or complex APIs, understanding static and dynamic routing helps you create clean, scalable Flask applications.
In this guide, you'll learn:
- What Flask routes are
- How routing decorators work
- The difference between static and dynamic routes
- How to use URL variables
- All built-in Flask variable converters (string, int, float, path, uuid)
- Code examples for each concept
What are Routes in Flask?
A route is a URL pattern defined by the developer that matches the URL requested by the browser or client. When a user visits a certain URL, Flask finds the matching route and executes the corresponding function (called a view function).
For example:
@app.route('/')
def home():
return "Homepage"
This route handles requests to /.
What are Routing Decorators in Flask?
In Flask, routes are created using routing decorators (@app.route, @app.get, @app.post, etc.). These decorators:
- Define the URL pattern
- Specify what HTTP method(s) the route accepts
- Link a URL to a view function
They allow Flask to respond to GET, POST, PUT, DELETE, and other HTTP requests.
List of Routing Decorators in Flask
Flask provides shortcut decorators for common HTTP methods:
| Shortcut Decorators | Equivalent Long Form |
|---|---|
app.get('/') |
app.route('/', methods=['GET']) |
app.post('/') |
app.route('/', methods=['POST']) |
app.patch('/') |
app.route('/', methods=['PATCH']) |
app.put('/') |
app.route('/', methods=['PUT']) |
app.delete('/') |
app.route('/', methods=['DELETE']) |
Using the shortcut version makes your code cleaner.
What is a Static Route in Flask?
A static route is a URL that never changes. It contains no dynamic segments.
Examples of static routes:
//about/contact
Flask will always return the same response for that specific URL.
Example of a Static Route
@app.route('/')
def hello():
return 'Hello, World!'
Visiting:
http://127.0.0.1:5000/
Returns:
<html>
<body>
Hello, World!
</body>
</html>
What are Dynamic Routes in Flask?
Dynamic routes contain variable parts, allowing the URL to change depending on the client's input.
These use angle brackets:
/<variable>
Flask captures the value and passes it to your view function.
Example of a Dynamic Route
@app.route('/<name>')
def hello(name=None):
return f"Hello, {name}!"
Visiting:
http://127.0.0.1:5000/yourname
Returns:
<html>
<body>
Hello, yourname!
</body>
</html>
What are URL Variables in Flask?
URL variables are placeholders inside the route pattern. They let you accept user-defined values inside the URL.
Example:
@app.route('/user/<username>')
These variables can also specify a type using variable converters.
URL Variable Types in Flask
Flask provides five built-in converters:
string– default, accepts text (no slashes)int– positive integersfloat– positive floating-point numberspath– like string, but allows slashesuuid– valid UUID values
Let's look at each.
1. String
Accepts any text without slashes. This is also the default converter.
@app.route("/user/<string:username>")
def show_user(username):
return f"Hello, {username}!"
Valid examples:
/user/dharyl/user/flask_blog
Invalid:
/user/john/doe(contains slash)
2. Int
Accepts positive integers only.
@app.route("/post/<int:post_id>")
def show_post(post_id):
return f"Post ID: {post_id}"
Examples:
/post/10→ Valid/post/0→ Valid/post/-5→ Invalid/post/abc→ Invalid
3. Path
Works like string but allows slashes.
@app.route("/file/<path:subpath>")
def show_subpath(subpath):
return f"Requested path: {subpath}"
Examples:
/file/images/logo.png→ Valid/file/docs/python/flask→ Valid/file/hello world→ Valid
4. Float
Accepts positive floating-point numbers.
@app.route("/price/<float:value>")
def show_price(value):
return f"Price: {value}"
Examples:
/price/19.99→ Valid/price/5→ Valid (converted to float)/price/-1.5→ Invalid/price/abc→ Invalid
5. UUID
Accepts valid UUID strings.
@app.route("/session/<uuid:session_id>")
def show_session(session_id):
return f"Session ID: {session_id}"
Valid:
/session/123e4567-e89b-12d3-a456-426614174000
Invalid:
/session/invalid-uuid
FAQs: Flask Routes, Static & Dynamic URL Routing
1. What is the difference between static and dynamic routes in Flask?
Static routes never change (e.g., /about, /contact).
Dynamic routes contain variables that accept input from the URL (e.g., /user/<username>).
2. Can a Flask route accept multiple URL parameters?
Yes. You can define multiple variables inside the same route:
@app.route('/user/<username>/post/<int:post_id>')
def user_post(username, post_id):
return f"{username} posted ID {post_id}"
3. What happens if two routes match the same URL pattern?
Flask will use the first matching route defined in your code. Order matters—define more specific routes before more general ones.
4. How do I restrict what type a URL variable accepts?
Use Flask’s variable converters such as int, float, path, or uuid.
Example:
@app.route('/item/<int:item_id>')
5. What is the default type for URL variables in Flask?
The default type is string, meaning:
@app.route('/user/<username>')
is the same as:
@app.route('/user/<string:username>')
6. Can I create custom variable converters in Flask?
Yes. Flask lets you build custom URL converters for advanced routing needs. This is useful for patterns like hex values, slugs, or special formats.
7. Can I use the same view function for multiple routes?
Absolutely. You can stack decorators:
@app.route('/')
@app.route('/home')
def homepage():
return "Welcome!"
8. How do I make a route respond to multiple HTTP methods?
You can specify methods like this:
@app.route('/submit', methods=['GET', 'POST'])
9. How do I return JSON instead of plain text?
Use jsonify:
from flask import jsonify
@app.route('/data')
def data():
return jsonify(message="Hello, JSON")
10. Are URL variables automatically validated?
Yes, but only by the converter type.
For example, /item/<int:id> ensures id is an integer.
For deeper validation, you must validate inside the function.