Skip to content

Commit

Permalink
Update docs to include friendly 404 feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeckennedy committed Jul 30, 2021
1 parent 503be47 commit 623c7ee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,26 @@ The view method should return a `dict` to be passed as variables/values to the t
If a `fastapi.Response` is returned, the template is skipped and the response along with status_code and
other values is directly passed through. This is common for redirects and error responses not meant
for this page template.

## Friendly 404s and errors

A common technique for user-friendly sites is to use a
[custom HTML page for 404 responses](http://www.instantshift.com/2019/10/16/user-friendly-404-pages/).
This is especially important in FastAPI because FastAPI returns a 404 response + JSON by default.
This library has support for friend 404 pages using the `fastapi_chameleon.not_found()` function.

Here's an example:

```python
@router.get('/')
@fastapi_chameleon.template('catalog/item.pt')
async def item(item_id: int):
item = service.get_item_by_id(item_id)
if not item:
fastapi_chameleon.not_found()

return item.dict()
```

This will render a 404 response with using the template file `templates/errors/404.pt`.
You can specify another template to use for the response, but it's not required.
2 changes: 1 addition & 1 deletion fastapi_chameleon/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""fastapi-chameleon - Adds integration of the Chameleon template language to FastAPI."""

__version__ = '0.1.10'
__version__ = '0.1.11'
__author__ = 'Michael Kennedy <michael@talkpython.fm>'
__all__ = ['template', 'global_init', 'not_found', ]

Expand Down

0 comments on commit 623c7ee

Please sign in to comment.