]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
📝 Add HTML media type to template docs (#1690)
authorFrancesco Frassinelli <fraph24@gmail.com>
Mon, 3 Aug 2020 07:53:56 +0000 (09:53 +0200)
committerGitHub <noreply@github.com>
Mon, 3 Aug 2020 07:53:56 +0000 (09:53 +0200)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
docs/en/docs/advanced/templates.md
docs_src/templates/tutorial001.py

index 871f3163f0622f57d608aa60b17ddb10c0954e5d..59d2735cb7f39c2bf70019de854d2a39ac051e13 100644 (file)
@@ -39,13 +39,16 @@ $ pip install aiofiles
 * 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".
 
-```Python hl_lines="3  10  14 15"
+```Python hl_lines="4  11  15 16"
 {!../../../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*.
 
+!!! tip
+    By declaring `response_class=HTMLResponse` the docs UI will be able to know that the response will be HTML.
+
 !!! note "Technical Details"
     You could also use `from starlette.templating import Jinja2Templates`.
 
index b818861db77e9e4a4e5094335814d25ed41def57..245e7110b195d257b8a93b063124465627f1f765 100644 (file)
@@ -1,4 +1,5 @@
 from fastapi import FastAPI, Request
+from fastapi.responses import HTMLResponse
 from fastapi.staticfiles import StaticFiles
 from fastapi.templating import Jinja2Templates
 
@@ -10,6 +11,6 @@ app.mount("/static", StaticFiles(directory="static"), name="static")
 templates = Jinja2Templates(directory="templates")
 
 
-@app.get("/items/{id}")
+@app.get("/items/{id}", response_class=HTMLResponse)
 async def read_item(request: Request, id: str):
     return templates.TemplateResponse("item.html", {"request": request, "id": id})