]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
📝 Update template docs with more info about `url_for` (#5937)
authorEzzeddin Abdullah <ezzeddinabdullah@gmail.com>
Thu, 11 Jan 2024 22:25:37 +0000 (00:25 +0200)
committerGitHub <noreply@github.com>
Thu, 11 Jan 2024 22:25:37 +0000 (23:25 +0100)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
docs/en/docs/advanced/templates.md
docs_src/templates/templates/item.html
tests/test_tutorial/test_templates/test_tutorial001.py

index 583abda7fb0e4787125a56f2d3e40d0e446f83cf..6055b30170db67f5189b6d570aa0a6481bb09e95 100644 (file)
@@ -46,21 +46,61 @@ $ pip install jinja2
 
 ## Writing templates
 
-Then you can write a template at `templates/item.html` with:
+Then you can write a template at `templates/item.html` with, for example:
 
 ```jinja hl_lines="7"
 {!../../../docs_src/templates/templates/item.html!}
 ```
 
-It will show the `id` taken from the "context" `dict` you passed:
+### Template Context Values
+
+In the HTML that contains:
+
+{% raw %}
+
+```jinja
+Item ID: {{ id }}
+```
+
+{% endraw %}
+
+...it will show the `id` taken from the "context" `dict` you passed:
 
 ```Python
-{"request": request, "id": id}
+{"id": id}
+```
+
+For example, with an ID of `42`, this would render:
+
+```html
+Item ID: 42
+```
+
+### Template `url_for` Arguments
+
+You can also use `url_for()` inside of the template, it takes as arguments the same arguments that would be used by your *path operation function*.
+
+So, the section with:
+
+{% raw %}
+
+```jinja
+<a href="{{ url_for('read_item', id=id) }}">
+```
+
+{% endraw %}
+
+...will generate a link to the same URL that would be handled by the *path operation function* `read_item(id=id)`.
+
+For example, with an ID of `42`, this would render:
+
+```html
+<a href="/items/42">
 ```
 
 ## Templates and static files
 
-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 with the `name="static"`.
 
 ```jinja hl_lines="4"
 {!../../../docs_src/templates/templates/item.html!}
index a70287e77d345d5869e5357703c2e5a830edc4dd..27994ca994b001ea8ae087a5e3e89d201b5874b0 100644 (file)
@@ -4,6 +4,6 @@
     <link href="{{ url_for('static', path='/styles.css') }}" rel="stylesheet">
 </head>
 <body>
-    <h1>Item ID: {{ id }}</h1>
+    <h1><a href="{{ url_for('read_item', id=id) }}">Item ID: {{ id }}</a></h1>
 </body>
 </html>
index bfee5c0902dbc30fdb43749599bf9f625ee7a62a..4d4729425e845247a7813356578075cd956387ec 100644 (file)
@@ -16,7 +16,10 @@ def test_main():
     client = TestClient(app)
     response = client.get("/items/foo")
     assert response.status_code == 200, response.text
-    assert b"<h1>Item ID: foo</h1>" in response.content
+    assert (
+        b'<h1><a href="http://testserver/items/foo">Item ID: foo</a></h1>'
+        in response.content
+    )
     response = client.get("/static/styles.css")
     assert response.status_code == 200, response.text
     assert b"color: green;" in response.content