Type Converters: int, float, string, path
Constrain URL segments with built-in converters.
Strings Are Not Always Enough
Captured values default to strings, but /post/42 should give a number, not text. A converter tells Flask what type to expect in that segment.
Converter Syntax
Put the converter name before the variable with a colon. The pattern is <converter:name>, and Flask applies it before calling your view.
@app.route('/post/<int:post_id>')
def show(post_id):
return str(post_id + 1)All lessons in this course
- Capture Variables from the Path
- Type Converters: int, float, string, path
- Build URLs with url_for
- Trailing Slashes and Redirect Behavior