From c55f90df32101006ad3ddd8060d30f24c8f44eb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Tue, 26 Dec 2023 21:12:34 +0100 Subject: [PATCH] =?UTF-8?q?=E2=AC=86=EF=B8=8F=20Upgrade=20Starlette=20to?= =?UTF-8?q?=20`>=3D0.29.0,<0.33.0`,=20update=20docs=20and=20usage=20of=20t?= =?UTF-8?q?emplates=20with=20new=20Starlette=20arguments=20(#10846)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * πŸ“ Update docs for compatibility with Starlette 0.29.0 and new template arguments * ⬆️ Upgrade Starlette to >=0.29.0,<0.33.0 * πŸ“Œ Remove AnyIO pin --- docs/em/docs/advanced/templates.md | 2 +- docs/en/docs/advanced/templates.md | 10 ++++++---- docs_src/templates/tutorial001.py | 4 +++- pyproject.toml | 4 +--- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/docs/em/docs/advanced/templates.md b/docs/em/docs/advanced/templates.md index 1fb57725a..0a73a4f47 100644 --- a/docs/em/docs/advanced/templates.md +++ b/docs/em/docs/advanced/templates.md @@ -27,7 +27,7 @@ $ pip install jinja2 * πŸ“£ `Request` πŸ”’ *➑ πŸ› οΈ* πŸ‘ˆ πŸ”œ πŸ“¨ πŸ“„. * βš™οΈ `templates` πŸ‘† ✍ ✍ & πŸ“¨ `TemplateResponse`, πŸšΆβ€β™€οΈ `request` 1️⃣ πŸ”‘-πŸ’² πŸ‘« Jinja2️⃣ "πŸ”‘". -```Python hl_lines="4 11 15-16" +```Python hl_lines="4 11 15-18" {!../../../docs_src/templates/tutorial001.py!} ``` diff --git a/docs/en/docs/advanced/templates.md b/docs/en/docs/advanced/templates.md index 38618aeeb..583abda7f 100644 --- a/docs/en/docs/advanced/templates.md +++ b/docs/en/docs/advanced/templates.md @@ -25,14 +25,16 @@ $ pip install jinja2 * Import `Jinja2Templates`. * 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". +* Use the `templates` you created to render and return a `TemplateResponse`, pass the name of the template, the request object, and a "context" dictionary with key-value pairs to be used inside of the Jinja2 template. -```Python hl_lines="4 11 15-16" +```Python hl_lines="4 11 15-18" {!../../../docs_src/templates/tutorial001.py!} ``` !!! 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*. + Before FastAPI 0.108.0, Starlette 0.29.0, the `name` was the first parameter. + + Also, before that, in previous versions, the `request` object was passed as part of the key-value pairs in the context for Jinja2. !!! tip By declaring `response_class=HTMLResponse` the docs UI will be able to know that the response will be HTML. @@ -58,7 +60,7 @@ It will show the `id` taken from the "context" `dict` you passed: ## 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. +You can also use `url_for()` inside of the template, and use it, for example, with the `StaticFiles` you mounted. ```jinja hl_lines="4" {!../../../docs_src/templates/templates/item.html!} diff --git a/docs_src/templates/tutorial001.py b/docs_src/templates/tutorial001.py index 245e7110b..81ccc8d4d 100644 --- a/docs_src/templates/tutorial001.py +++ b/docs_src/templates/tutorial001.py @@ -13,4 +13,6 @@ templates = Jinja2Templates(directory="templates") @app.get("/items/{id}", response_class=HTMLResponse) async def read_item(request: Request, id: str): - return templates.TemplateResponse("item.html", {"request": request, "id": id}) + return templates.TemplateResponse( + request=request, name="item.html", context={"id": id} + ) diff --git a/pyproject.toml b/pyproject.toml index 499d0e5dc..38728d99e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,11 +40,9 @@ classifiers = [ "Topic :: Internet :: WWW/HTTP", ] dependencies = [ - "starlette>=0.28.0,<0.29.0", + "starlette>=0.29.0,<0.33.0", "pydantic>=1.7.4,!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,!=2.1.0,<3.0.0", "typing-extensions>=4.8.0", - # TODO: remove this pin after upgrading Starlette 0.31.1 - "anyio>=3.7.1,<4.0.0", ] dynamic = ["version"]