From 5110fc6d81733a2e3f32e62b1da036709a84c13a Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sat, 14 Mar 2020 17:51:50 -0700 Subject: [PATCH] More detail on how to use convertors It wasn't immediately clear to me how to use them. --- docs/routing.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/docs/routing.md b/docs/routing.md index 703d6b13..3489880a 100644 --- a/docs/routing.md +++ b/docs/routing.md @@ -36,11 +36,21 @@ Paths can use URI templating style to capture path components. ```python Route('/users/{username}', user) ``` +By default this will capture characters up to the end of the path or the next `/`. -Convertors for `int`, `float`, and `path` are also available: +You can use convertors to modify what is captured. Four convertors are available: + +* `str` returns a string, and is the default. +* `int` returns a Python integer. +* `float` returns a Python float. +* `path` returns the rest of the path, including any additional `/` characers. + +Convertors are used by prefixing them with a colon, like so: ```python Route('/users/{user_id:int}', user) +Route('/floating-point/{number:float}', floating_point) +Route('/uploaded/{rest_of_path:path}', uploaded) ``` Path parameters are made available in the request, as the `request.path_params` -- 2.47.2