Templating - Jinja2Templates

You can use the Jinja2Templates class to render Jinja templates.

Read more about it in the FastAPI docs for Templates.

You can import it directly from fastapi.templating:

  1. from fastapi.templating import Jinja2Templates

fastapi.templating.Jinja2Templates

  1. Jinja2Templates(
  2. directory, context_processors=None, **env_options
  3. )

templates = Jinja2Templates(“templates”)

return templates.TemplateResponse(“index.html”, {“request”: request})

PARAMETERDESCRIPTION
directory

TYPE: Union[str, PathLike]

context_processors

TYPE: Optional[List[Callable[[Request], Dict[str, Any]]]] DEFAULT: None

**env_options

TYPE: Any DEFAULT: {}

Source code in starlette/templating.py

  1. 65
  2. 66
  3. 67
  4. 68
  5. 69
  6. 70
  7. 71
  8. 72
  9. 73
  10. 74
  11. 75
  1. def init(
  2. self,
  3. directory: typing.Union[str, PathLike],
  4. context_processors: typing.Optional[
  5. typing.List[typing.Callable[[Request], typing.Dict[str, typing.Any]]]
  6. ] = None,
  7. env_options: typing.Any,
  8. ) -> None:
  9. assert jinja2 is not None, jinja2 must be installed to use Jinja2Templates
  10. self.env = self._create_env(directory, env_options)
  11. self.context_processors = context_processors or []

env instance-attribute

  1. env = _create_env(directory, **env_options)

context_processors instance-attribute

  1. context_processors = context_processors or []

get_template

  1. get_template(name)
PARAMETERDESCRIPTION
name

TYPE: str

Source code in starlette/templating.py

  1. 93
  2. 94
  1. def get_template(self, name: str) -> jinja2.Template:
  2. return self.env.get_template(name)

TemplateResponse

  1. TemplateResponse(
  2. name,
  3. context,
  4. status_code=200,
  5. headers=None,
  6. media_type=None,
  7. background=None,
  8. )
PARAMETERDESCRIPTION
name

TYPE: str

context

TYPE: dict

status_code

TYPE: int DEFAULT: 200

headers

TYPE: Optional[Mapping[str, str]] DEFAULT: None

media_type

TYPE: Optional[str] DEFAULT: None

background

TYPE: Optional[BackgroundTask] DEFAULT: None

Source code in starlette/templating.py

  1. 96
  2. 97
  3. 98
  4. 99
  5. 100
  6. 101
  7. 102
  8. 103
  9. 104
  10. 105
  11. 106
  12. 107
  13. 108
  14. 109
  15. 110
  16. 111
  17. 112
  18. 113
  19. 114
  20. 115
  21. 116
  22. 117
  23. 118
  24. 119
  25. 120
  1. def TemplateResponse(
  2. self,
  3. name: str,
  4. context: dict,
  5. status_code: int = 200,
  6. headers: typing.Optional[typing.Mapping[str, str]] = None,
  7. media_type: typing.Optional[str] = None,
  8. background: typing.Optional[BackgroundTask] = None,
  9. ) -> _TemplateResponse:
  10. if request not in context:
  11. raise ValueError(context must include a request key)
  12. request = typing.cast(Request, context[request])
  13. for context_processor in self.context_processors:
  14. context.update(context_processor(request))
  15. template = self.get_template(name)
  16. return _TemplateResponse(
  17. template,
  18. context,
  19. status_code=status_code,
  20. headers=headers,
  21. media_type=media_type,
  22. background=background,
  23. )