📝 add missing info about error routes
This commit is contained in:
parent
d14bf8ae14
commit
80e70f8843
@ -87,4 +87,66 @@ routes = [
|
||||
("/submit", submit_view, {"allowed_methods": ["POST"], "csrf_exempt": True})
|
||||
]
|
||||
```
|
||||
Note that passing in `csrf_exempt` is not listed on the other two methods, mostly because it doesn't really make sense for the other methods. Instead, they use a decorator to handle it, which can be found in [the docs for CSRF protection.](middleware/csrf.md?id=marking-views-as-csrf-exempt) You can also use the decorator in the same way for routes assigned in this manner, but when you have a large number of routes, being able to see all the attributes in one place is helpful.
|
||||
Note that passing in `csrf_exempt` is not listed on the other two methods, mostly because it doesn't really make sense for the other methods. Instead, they use a decorator to handle it, which can be found in [the docs for CSRF protection.](middleware/csrf.md?id=marking-views-as-csrf-exempt) You can also use the decorator in the same way for routes assigned in this manner, but when you have a large number of routes, being able to see all the attributes in one place is helpful.
|
||||
|
||||
## Passing Data Through Routes
|
||||
|
||||
Some views need to be able to take arguments via the URL path, so Spiderweb provides that ability for you. The syntax used is identical to Django's: `/routename/<str:argname>`. In this case, it will slice out that part of the URL, cast it to a string, and pass it as a variable named `argname` to your view. Here's what that looks like in practice:
|
||||
|
||||
```python
|
||||
@app.route("/example/<int:id>")
|
||||
def example(request, id): # <- note the additional arg!
|
||||
return HttpResponse(body=f"Example with id {id}")
|
||||
```
|
||||
You can pass integers, strings, and positive floats with the following types:
|
||||
|
||||
- str
|
||||
- int
|
||||
- float
|
||||
|
||||
A URL can also have multiple capture groups:
|
||||
|
||||
```python
|
||||
@app.route("/example/<int:id>/<str:name>")
|
||||
def example(request, id, name):
|
||||
return HttpResponse(body=f"Example with id {id} and name {name}")
|
||||
```
|
||||
In this case, a valid URL might be `/example/3/james`, and both sections will be split out and passed to the view.
|
||||
|
||||
## 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.
|
||||
|
||||
An error view has three pieces: the request, the response, and what error code triggers it. The easiest way to do this is with the decorator.
|
||||
|
||||
### `.error` Decorator
|
||||
|
||||
```python
|
||||
@app.error(405)
|
||||
def http405(request) -> HttpResponse:
|
||||
return HttpResponse(body="Method not allowed", status_code=405)
|
||||
```
|
||||
Note that this is just a basic view with nothing really special about it; the only real difference is that the `.error()` decorator highlights the specific error that will trigger this view. If the server, at any point, hits this error value, it will retrieve this view and return it instead of the requested view.
|
||||
|
||||
### After Instantiation
|
||||
|
||||
Similar to the regular views, error views can also be added programmatically at runtime like this:
|
||||
|
||||
```python
|
||||
def http405(request) -> HttpResponse:
|
||||
return HttpResponse(body="Method not allowed", status_code=405)
|
||||
|
||||
app.add_error_route(405, http405)
|
||||
```
|
||||
No other attributes or arguments are available.
|
||||
|
||||
### During Instantiation
|
||||
|
||||
For those with larger numbers of routes, it may make more sense to declare them when the server object is built. For example:
|
||||
|
||||
```python
|
||||
app = SpiderwebRouter(
|
||||
error_routes={405: http405},
|
||||
)
|
||||
```
|
||||
As with the `routes` argument, as many routes as you'd like can be registered here without issue.
|
@ -34,6 +34,11 @@ def index(request):
|
||||
return TemplateResponse(request, "test.html", context={"value": "TEST!"})
|
||||
|
||||
|
||||
@app.route("/example/<int:id>/<str:name>")
|
||||
def example(request, id, name):
|
||||
return HttpResponse(body=f"Example with id {id} and name {name}")
|
||||
|
||||
|
||||
@app.route("/redirect")
|
||||
def redirect(request):
|
||||
return RedirectResponse("/")
|
||||
|
Loading…
Reference in New Issue
Block a user