Files
fastapi/docs/advanced/templates.md
Sebastián Ramírez 0ac9b3ee5c Re-export utils from Starlette (#1064)
*  Re-export main features used from Starlette to simplify developer's code

* ♻️ Refactor Starlette exports

* ♻️ Refactor tutorial examples to use re-exported utils from Starlette

* 📝 Add examples for all middlewares

* 📝 Add new docs for middlewares

* 📝 Add examples for custom responses

* 📝 Extend docs for custom responses

* 📝 Update docs and add notes explaining re-exports from Starlette everywhere

* 🍱 Update screenshot for HTTP status

* 🔧 Update MkDocs config with new content

* ♻️ Refactor tests to use re-exported utils from Starlette

*  Re-export WebSocketDisconnect from Starlette for tests

*  Add extra tests for extra re-exported middleware

*  Add tests for re-exported responses from Starlette

*  Add docs about mounting WSGI apps

*  Add Flask as a dependency to test WSGIMiddleware

*  Test WSGIMiddleware example
2020-03-01 21:49:20 +01:00

2.3 KiB

You can use any template engine you want with FastAPI.

A common election is Jinja2, the same one used by Flask and other tools.

There are utilities to configure it easily that you can use directly in your FastAPI application (provided by Starlette).

Install dependencies

Install jinja2:

pip install jinja2

If you need to also serve static files (as in this example), install aiofiles:

pip install aiofiles

Using Jinja2Templates

  • Import Jinja2Templates.
  • Create a templates object that you can re-use later.
  • Declare a Request parameter in the path operation that will return a template.
  • Use the templates you created to render and return a TemplateResponse, passing the request as one of the key-value pairs in the Jinja2 "context".
{!./src/templates/tutorial001.py!}

!!! note Notice that you have to pass the request as part of the key-value pairs in the context for Jinja2. So, you also have to declare it in your path operation.

!!! note "Technical Details" You could also use from starlette.templating import Jinja2Templates.

**FastAPI** provides the same `starlette.templating` as `fastapi.templating` just as a convenience for you, the developer. But most of the available responses come directly from Starlette. The same with `Request` and `StaticFiles`.

Writing templates

Then you can write a template at templates/item.html with:

{!./src/templates/templates/item.html!}

It will show the id taken from the "context" dict you passed:

{"request": request, "id": id}

Templates and static files

And you can also use url_for() inside of the template, and use it, for example, with the StaticFiles you mounted.

{!./src/templates/templates/item.html!}

In this example, it would link to a CSS file at static/styles.css with:

{!./src/templates/static/styles.css!}

And because you are using StaticFiles, that CSS file would be served automatically by your FastAPI application at the URL /static/styles.css.

More details

For more details, including how to test templates, check Starlette's docs on templates.