How to Use URL Converters in Flask for Dynamic Routes
In Flask, use
url converters in route parameters to capture and validate dynamic parts of URLs, like integers or strings. Place the converter type in angle brackets, for example <int:id>, to ensure the URL part matches the expected type before passing it to your view function.Syntax
Flask URL converters are used inside route parameters to specify the expected type of the variable part of the URL. The syntax is <converter:name>, where converter is the type and name is the variable name passed to the view function.
Common converters include:
string: (default) accepts any text except slashesint: accepts integersfloat: accepts floating point numberspath: like string but accepts slashesuuid: accepts UUID strings
python
@app.route('/user/<int:user_id>') def user_profile(user_id): pass
Example
This example shows a Flask app with routes using different URL converters. It demonstrates how Flask passes the converted URL parts as typed arguments to the view functions.
python
from flask import Flask app = Flask(__name__) @app.route('/item/<int:item_id>') def show_item(item_id): return f"Item ID is {item_id} and its type is {type(item_id).__name__}" @app.route('/price/<float:price>') def show_price(price): return f"Price is {price} and its type is {type(price).__name__}" @app.route('/path/<path:subpath>') def show_subpath(subpath): return f"Subpath is '{subpath}'" if __name__ == '__main__': app.run(debug=True)
Output
When visiting /item/42, output: Item ID is 42 and its type is int
When visiting /price/19.99, output: Price is 19.99 and its type is float
When visiting /path/folder1/folder2, output: Subpath is 'folder1/folder2'
Common Pitfalls
- Not specifying a converter defaults to
string, which may cause unexpected behavior if you expect numbers. - Using
intconverter but passing a non-integer in the URL causes a 404 error. - For paths with slashes, use
pathconverter instead ofstring. - For UUIDs, use the
uuidconverter to validate format automatically.
python
from flask import Flask app = Flask(__name__) # Wrong: expects int but URL has string @app.route('/user/<int:user_id>') def user(user_id): return f"User {user_id}" # Correct: use string if user_id can be alphanumeric @app.route('/user/<string:user_id>') def user_str(user_id): return f"User {user_id}"
Quick Reference
| Converter | Description | Example URL Part |
|---|---|---|
| string | Default, accepts any text except slash | hello-world |
| int | Accepts integers | 123 |
| float | Accepts floating point numbers | 3.14 |
| path | Like string but accepts slashes | folder/subfolder/file |
| uuid | Accepts UUID strings | 550e8400-e29b-41d4-a716-446655440000 |
Key Takeaways
Use
<converter:name> syntax in Flask routes to capture typed URL parts.Common converters include string, int, float, path, and uuid for flexible URL matching.
Incorrect converter usage can cause 404 errors if URL parts don't match expected types.
Use
path converter to capture URL parts containing slashes.URL converters help validate and convert URL parameters automatically before your code uses them.