📝 update routes.md to mention the new path converter

This commit is contained in:
Joe Kaufeld 2024-09-16 18:22:29 -04:00
parent 19ff69e999
commit 889b62d4f9

View File

@ -103,6 +103,7 @@ You can pass integers, strings, and positive floats with the following types:
- str - str
- int - int
- float - float
- path (see below)
A URL can also have multiple capture groups: A URL can also have multiple capture groups:
@ -113,6 +114,15 @@ def example(request, id, name):
``` ```
In this case, a valid URL might be `/example/3/james`, and both sections will be split out and passed to the view. In this case, a valid URL might be `/example/3/james`, and both sections will be split out and passed to the view.
The `path` option is special; this is used when you want to capture everything after the slash. For example:
```python
@app.route("/example/<path:rest>")
def example(request, rest):
return HttpResponse(body=f"Example with {rest}")
```
It will come in as a string, but it will include all the slashes and other characters that are in the URL.
## Adding Error Views ## Adding Error Views
For some apps, you may want to have your own error views that are themed to your particular application. For this, there's a slightly different process, but the gist is the same. There are also three ways to handle error views, all very similar to adding regular views. For some apps, you may want to have your own error views that are themed to your particular application. For this, there's a slightly different process, but the gist is the same. There are also three ways to handle error views, all very similar to adding regular views.
@ -184,4 +194,4 @@ path = app.reverse("example", {'obj_id': 3}, query={'name': 'james'})
print(path) # -> "/example/3?name=james" print(path) # -> "/example/3?name=james"
``` ```
The arguments you pass in must match what the path expects, or you'll get a `SpiderwebException`. If there's no route with that name, you'll get a `ReverseNotFound` exception instead. The arguments you pass in must match what the path expects, or you'll get a `SpiderwebException`. If there's no route with that name, you'll get a `ReverseNotFound` exception instead.