FastAPI templates: Jinja2Templates and TemplateResponse
Answer: FastAPI can render HTML with Jinja2Templates. Import Jinja2Templates, point it at a templates directory, and return a TemplateResponse with the template name, the request, and a context.
Examples
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
templates = Jinja2Templates(directory="templates")
Notes
You can use any template engine, but the tutorial uses Jinja2Templates.
Reference list
Thepage is about templates in FastAPI.UseJinja2Templates to render templates.Returna TemplateResponse with the template name.
See also
Claims
- id: C1 claim: The page is about templates in FastAPI. source: S2 evidence: "Templates"
- id: C2 claim: Use Jinja2Templates to render templates. source: S1 evidence: "Jinja2Templates"
- id: C3 claim: Return a TemplateResponse with the template name. source: S2 evidence: "return a
TemplateResponse"
Sources
- [S1] Templates. FastAPI docs. https://fastapi.tiangolo.com/advanced/templates/ . License: MIT. Accessed 2026-11-18.
- [S2] templates.md. FastAPI. https://raw.githubusercontent.com/fastapi/fastapi/master/docs/en/docs/advanced/templates.md . License: MIT. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Templates; FastAPI, templates.md. MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright FastAPI contributors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Templates; FastAPI, templates.md. MIT (https://raw.githubusercontent.com/fastapi/fastapi/master/LICENSE). Copyright FastAPI contributors.
Cite this
iinventedlight. 2026. "FastAPI templates: Jinja2Templates and TemplateResponse." iinventedlight Library, iil:note:pvjspgpx, version of 2026-11-18. https://iinventedlight.com/library/notes/fastapi-templates/. License: CC-BY-4.0.
@misc{iil_note_pvjspgpx,
author = {{iinventedlight}},
title = {{FastAPI templates: Jinja2Templates and TemplateResponse}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/fastapi-templates/},
note = {Permanent ID iil:note:pvjspgpx. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- fastapi-templates.table.json (table; CC-BY-4.0)
- fastapi-templates.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/fastapi-templates.md.