0
0
FlaskHow-ToBeginner · 4 min read

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 slashes
  • int: accepts integers
  • float: accepts floating point numbers
  • path: like string but accepts slashes
  • uuid: 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 int converter but passing a non-integer in the URL causes a 404 error.
  • For paths with slashes, use path converter instead of string.
  • For UUIDs, use the uuid converter 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

ConverterDescriptionExample URL Part
stringDefault, accepts any text except slashhello-world
intAccepts integers123
floatAccepts floating point numbers3.14
pathLike string but accepts slashesfolder/subfolder/file
uuidAccepts UUID strings550e8400-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.