From: Simon Willison Date: Sun, 15 Mar 2020 00:51:50 +0000 (-0700) Subject: More detail on how to use convertors X-Git-Tag: 0.13.3~8^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F862%2Fhead;p=thirdparty%2Fstarlette.git More detail on how to use convertors It wasn't immediately clear to me how to use them. --- 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`