Templates

You can use any template engine you want with FastAPI.

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

Starlette has utilities to configure it easily that you can use directly in your FastAPI application.

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 form Starlette.
  • 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".
from fastapi import FastAPI
from starlette.requests import Request
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates

app = FastAPI()

app.mount("/static", StaticFiles(directory="static"), name="static")


templates = Jinja2Templates(directory="templates")


@app.get("/items/{id}")
async def read_item(request: Request, id: str):
    return templates.TemplateResponse("item.html", {"request": request, "id": id})

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.

Writing templates

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

<html>
<head>
    <title>Item Details</title>
    <link href="{{ url_for('static', path='/styles.css') }}" rel="stylesheet">
</head>
<body>
    <h1>Item ID: {{ id }}</h1>
</body>
</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.

<html>
<head>
    <title>Item Details</title>
    <link href="{{ url_for('static', path='/styles.css') }}" rel="stylesheet">
</head>
<body>
    <h1>Item ID: {{ id }}</h1>
</body>
</html>

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

h1 {
    color: green;
}

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.