Jinja2 Templates
In this chapter you'll learn to configure the Jinja2 template engine in FastAPI and use static files and template inheritance to build complete HTML pages.
FastAPI's two rendering modes
FastAPI returns JSON by default (suitable for APIs), but can also return HTML (suitable for SSR).
Both modes can coexist—the same project can provide a JSON API and render web pages.
Mounting static files
Static files (CSS, JS, images) are StaticFiles mounted at a specified path.
Example
File path: main.py
from
fastapi
import
FastAPI
from
fastapi.
staticfiles
import
StaticFiles
app
=
FastAPI
(
)
Mount the static/ directory at the /static path
Accessing /static/css/style.css → actually reads static/css/style.css
app.
mount
(
"/static"
,
StaticFiles
(
directory
=
"static"
)
,
name
=
"static"
)
Referencing static files in templates:
Example
Configure Jinja2 templates
Example
File path: main.py
from
fastapi
import
FastAPI
,
Request
from
fastapi.
staticfiles
import
StaticFiles
from
fastapi.
templating
import
Jinja2Templates
app
=
FastAPI
(
)
app.
mount
(
"/static"
,
StaticFiles
(
directory
=
"static"
)
,
name
=
"static"
)
Specify the template directory
templates
=
Jinja2Templates
(
directory
=
"templates"
)
@
app.
get
(
"/"
)
def
index
(
request: Request
)
:
Note: template routes must accept a Request parameter
"""Blog homepage"""
return
templates.
TemplateResponse
(
"index.html"
,
Template filename
{
context data
"request"
: request
,
request must be passed in (required by Jinja2Templates)
"title"
:
"RUNOOB Blog"
}
)
There are two key differences between FastAPI's template rendering and Flask's: 1. the view function must accept a
request: Requestparameter; 2.requestmust be passed to the template as context. If request is omitted, url_for in the template won't work.
Create the base.html parent template
Example
{% block title %}RUNOOB Blog{% endblock %}
{% block extra_head %}{% endblock %}
RUNOOB Blog (FastAPI)
{% block content %}{% endblock %}
Create the index.html child template
Example
{% extends 'base.html' %}
{% block title %}{{ title }}{% endblock %}
{% block content %}
Latest Articles
The article list will be shown in later chapters.
{% endblock %}
url_for() in FastAPI templates
Similar to Flask, FastAPI also supports url_for(), but you need to name the route first.
Example
@
app.
get
(
"/post/{post_id}"
,
name
=
"post_detail"
)
name= names the route
def
post_detail
(
request: Request
,
post_id:
int
)
:
return
templates.
TemplateResponse
(
"post_detail.html"
,
{
"request"
: request
,
...
}
)
Example
Article 3
Creating basic CSS styles
Example
/* File path: static/css/style.css */
{
margin
:
0
;
padding
:
0
;
box-sizing
:
border-box
;
}
body
{
font-family
:
-apple-system
,
BlinkMacSystemFont
,
'Segoe UI'
,
Roboto
,
sans-serif
;
background
:
#f5f5f5
;
color
:
#333
;
}
.navbar
{
display
:
flex
;
justify-content
:
space-between
;
align-items
:
center
;
padding
:
16px
40px
;
background
:
#fff
;
box-shadow
:
0
2px
8px
rgba
(
0
,
0
,
0
,
0.1
)
;
}
.navbar
.logo
{
font-size
:
22px
;
font-weight
:
bold
;
color
:
#009688
;
text-decoration
:
none
;
}
.navbar
nav a
{
margin-left
:
20px
;
text-decoration
:
none
;
color
:
#333
;
}
.container
{
max-width
:
960px
;
margin
:
40px
auto
;
padding
:
0
20px
;
}
.footer
{
text-align
:
center
;
padding
:
20px
;
color
:
#999
;
border-top
:
1px
solid
#eee
;
}
.section-title
{
font-size
:
24px
;
margin-bottom
:
20px
;
}
Chapter Summary
In this chapter you learned front-end rendering configuration in FastAPI: StaticFiles for mounting static assets, Jinja2Templates for configuring the template directory, TemplateResponse for returning HTML, and the key difference from Flask in template rendering (the Request parameter must be passed in).