Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.7k
🌐 Add Chinese translation fordocs/zh/docs/advanced/templates.md#3812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
tiangolo merged 3 commits intofastapi:masterfromjaystone776:Add-Chinese-Translation-for-docs-advanced-templates.mdMar 30, 2024
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
94 changes: 94 additions & 0 deletionsdocs/zh/docs/advanced/templates.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # 模板 | ||
| **FastAPI** 支持多种模板引擎。 | ||
| Flask 等工具使用的 Jinja2 是最用的模板引擎。 | ||
| 在 Starlette 的支持下,**FastAPI** 应用可以直接使用工具轻易地配置 Jinja2。 | ||
| ## 安装依赖项 | ||
| 安装 `jinja2`: | ||
| <div class="termy"> | ||
| ```console | ||
| $ pip install jinja2 | ||
| ---> 100% | ||
| ``` | ||
| </div> | ||
| 如需使用静态文件,还要安装 `aiofiles`: | ||
| <div class="termy"> | ||
| ```console | ||
| $ pip install aiofiles | ||
| ---> 100% | ||
| ``` | ||
| </div> | ||
| ## 使用 `Jinja2Templates` | ||
| * 导入 `Jinja2Templates` | ||
| * 创建可复用的 `templates` 对象 | ||
| * 在返回模板的*路径操作*中声明 `Request` 参数 | ||
| * 使用 `templates` 渲染并返回 `TemplateResponse`, 以键值对方式在 Jinja2 的 **context** 中传递 `request` | ||
| ```Python hl_lines="4 11 15-16" | ||
| {!../../../docs_src/templates/tutorial001.py!} | ||
| ``` | ||
| !!! note "笔记" | ||
| 注意,必须为 Jinja2 以键值对方式在上下文中传递 `request`。因此,还要在*路径操作*中声明。 | ||
| !!! tip "提示" | ||
| 通过声明 `response_class=HTMLResponse`,API 文档就能识别响应的对象是 HTML。 | ||
| !!! note "技术细节" | ||
| 您还可以使用 `from starlette.templating import Jinja2Templates`。 | ||
| **FastAPI** 的 `fastapi.templating` 只是为开发者提供的快捷方式。实际上,绝大多数可用响应都直接继承自 Starlette。 `Request` 与 `StaticFiles` 也一样。 | ||
| ## 编写模板 | ||
| 编写模板 `templates/item.html`,代码如下: | ||
| ```jinja hl_lines="7" | ||
| {!../../../docs_src/templates/templates/item.html!} | ||
| ``` | ||
| 它会显示从 **context** 字典中提取的 `id`: | ||
| ```Python | ||
| {"request": request, "id": id} | ||
| ``` | ||
| ## 模板与静态文件 | ||
| 在模板内部使用 `url_for()`,例如,与挂载的 `StaticFiles` 一起使用。 | ||
| ```jinja hl_lines="4" | ||
| {!../../../docs_src/templates/templates/item.html!} | ||
| ``` | ||
| 本例中,使用 `url_for()` 为模板添加 CSS 文件 `static/styles.css` 链接: | ||
| ```CSS hl_lines="4" | ||
| {!../../../docs_src/templates/static/styles.css!} | ||
| ``` | ||
| 因为使用了 `StaticFiles`, **FastAPI** 应用自动提供位于 URL `/static/styles.css` | ||
| 的 CSS 文件。 | ||
| ## 更多说明 | ||
| 包括测试模板等更多详情,请参阅 <a href="https://www.starlette.io/templates/" class="external-link" target="_blank">Starlette 官档 - 模板</a>。 |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.