← All notes

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

See also

Claims

Sources

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

Permanent ID: iil:note:pvjspgpx

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.},
}

Markdown

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.

All artifact files (JSON index)

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.