Updated routing docs (#1620)

* Updated routing docs

Updated routing docs to show all supported types as defined within 3685b4de85/sanic/router.py (L18)
Added example code for all examples besides regex
Added examples of queries that work with that type and ones that would not

* Tweak to call out string not str

Related to https://github.com/huge-success/sanic/pull/1620#discussion_r300120962

* Changed to using code comments to achieve a mono space like display

To address https://github.com/huge-success/sanic/pull/1620#discussion_r300120726

* Adjusted to list

Following https://github.com/huge-success/sanic/pull/1620#discussion_r300120726
This commit is contained in:
Lagicrus 2019-07-04 13:14:10 +01:00 committed by Stephen Sadowski
parent 72b445621b
commit b7df86e7dd

View File

@ -41,27 +41,75 @@ inside the quotes. If the parameter does not match the specified type, Sanic
will throw a `NotFound` exception, resulting in a `404: Page not found` error will throw a `NotFound` exception, resulting in a `404: Page not found` error
on the URL. on the URL.
### Supported types
* `string`
* "Bob"
* "Python 3"
* `int`
* 10
* 20
* 30
* -10
* (No floats work here)
* `number`
* 1
* 1.5
* 10
* -10
* `alpha`
* "Bob"
* "Python"
* (If it contains a symbol or a non alphanumeric character it will fail)
* `path`
* "hello"
* "hello.text"
* "hello world"
* `uuid`
* 123a123a-a12a-1a1a-a1a1-1a12a1a12345 (UUIDv4 Support)
* `regex expression`
If no type is set then a string is expected. The argument given to the function will always be a string, independent of the type.
```python ```python
from sanic.response import text from sanic.response import text
@app.route('/number/<integer_arg:int>') @app.route('/string/<string_arg:string>')
async def string_handler(request, string_arg):
return text('String - {}'.format(string_arg))
@app.route('/int/<integer_arg:int>')
async def integer_handler(request, integer_arg): async def integer_handler(request, integer_arg):
return text('Integer - {}'.format(integer_arg)) return text('Integer - {}'.format(integer_arg))
@app.route('/number/<number_arg:number>') @app.route('/number/<number_arg:number>')
async def number_handler(request, number_arg): async def number_handler(request, number_arg):
return text('Number - {}'.format(number_arg)) return text('Number - {}'.format(number_arg))
@app.route('/alpha/<alpha_arg:alpha>')
async def number_handler(request, alpha_arg):
return text('Alpha - {}'.format(alpha_arg))
@app.route('/path/<path_arg:path>')
async def number_handler(request, path_arg):
return text('Path - {}'.format(path_arg))
@app.route('/uuid/<uuid_arg:uuid>')
async def number_handler(request, uuid_arg):
return text('Uuid - {}'.format(uuid_arg))
@app.route('/person/<name:[A-z]+>') @app.route('/person/<name:[A-z]+>')
async def person_handler(request, name): async def person_handler(request, name):
return text('Person - {}'.format(name)) return text('Person - {}'.format(name))
@app.route('/folder/<folder_id:[A-z0-9]{0,4}>') @app.route('/folder/<folder_id:[A-z0-9]{0,4}>')
async def folder_handler(request, folder_id): async def folder_handler(request, folder_id):
return text('Folder - {}'.format(folder_id)) return text('Folder - {}'.format(folder_id))
``` ```
**Warning** `str` is not a valid type tag. If you want `str` recognition then you must use `string`
## HTTP request types ## HTTP request types
By default, a route defined on a URL will be available for only GET requests to that URL. By default, a route defined on a URL will be available for only GET requests to that URL.