惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google Online Security Blog
Google Online Security Blog
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
Jina AI
Jina AI
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
V
V2EX
雷峰网
雷峰网
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
F
Full Disclosure
Y
Y Combinator Blog
V
V2EX - 技术
Attack and Defense Labs
Attack and Defense Labs
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
SecWiki News
SecWiki News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
量子位
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AWS News Blog
AWS News Blog
Blog — PlanetScale
Blog — PlanetScale
爱范儿
爱范儿
K
Kaspersky official blog
B
Blog
A
Arctic Wolf
Hacker News: Ask HN
Hacker News: Ask HN
L
LangChain Blog
T
Tor Project blog
P
Privacy & Cybersecurity Law Blog
Recent Announcements
Recent Announcements
宝玉的分享
宝玉的分享
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
Lohrmann on Cybersecurity
D
Docker
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
The Last Watchdog
The Last Watchdog
S
Security Affairs
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Privacy International News Feed
Simon Willison's Weblog
Simon Willison's Weblog

博客园 - 全栈测试笔记

博文阅读密码验证 - 博客园 FastAPI系列(24):ORM操作之删除接口开发 单例模式管理模型客户端的几种实现方式 博文阅读密码验证 - 博客园 FastAPI系列(23):ORM操作之编辑接口开发 FastAPI系列(22):ORM之多对多查询记录 FastAPI系列(21):ORM之多对多添加记录 CC Switch配置模型 Claude Code安装 FastAPI系列(20):ORM添加表记录 FastAPI系列(19):ORM响应页面数据 博文阅读密码验证 - 博客园 FastAPI系列(18):ORM查询操作 FastAPI系列(17):ORM的迁移命令 FastAPI系列(16):ORM创建模型类 FastAPI系列(15):Jinja2模板语法之控制结构 FastAPI系列(14):Jinja2模板语法之过滤器 FastAPI系列(12):响应模型参数 【汇总】FastAPI系列教程
FastAPI系列(13):Jinja2模板介绍、模板语法之变量渲染
全栈测试笔记 · 2026-01-26 · via 博客园 - 全栈测试笔记

本系列汇总,请查看这里https://www.cnblogs.com/uncleyong/p/19503695

Jinja2模板介绍

模板在Python的web开发中⼴泛使⽤,它能够有效的将业务逻辑和页⾯逻辑分开,使代码可读性增强、并且更加容易理解和维护。

模板简单来说就是⼀个包含占位变量的⽂件,占位变量表⽰动态的部分,模板⽂件在经过动态赋值后,返回给⽤户。

jinja2是Flask作者开发的⼀个模板系统,起初是仿django模板的⼀个模板引擎,为Flask提供模板⽀持,由于其灵活,快速和安全等优点被⼴泛使⽤。

用到Jinja2模板,客户端看到的内容和所有界面效果都是由服务端提供的,这就是前后端不分离的应用模式。

在jinja2中语法:

1. 变量取值 {{ }}
2. 控制结构 {% %}

安装

pip install jinja2

test_jinja2.py同目录下创建

image

变量渲染:示例1

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>Welcome :{{ user}}</p>
</body>
</html>

test_jinja2.py

import uvicorn
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates

app = FastAPI()

templates = Jinja2Templates(directory="templates")  # 实例化Jinja2对象,并将文件夹路径设置为以templates命令的文件夹


@app.get("/index")
def index(request: Request):  # TemplateResponse要求上下文包含Request类型的对象,这里需要传入
    name = "韧"
    return templates.TemplateResponse(
        "index.html",  # 模板文件
        {
            "user": name,
            "request": request,  # TemplateResponse要求上下文(context)中必须包含 "request" 键,value是Request类型对象
        },  # context上下文对象,一个字典
    )


if __name__ == '__main__':
    uvicorn.run("test_jinja2:app", port=8001, reload=True)

接口文档请求

image

可以可能到响应头是text/html

浏览器请求

image

变量渲染:示例2

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>Welcome :{{ user}}</p>
<p>Job :{{ job }}</p>
<p>hobbies :{{ hobbies }}</p>
<p>hobby_1 :{{ hobbies.0 }}</p>
<p>hobby_2 :{{ hobbies.1 }}</p>
<p>hobby_3 :{{ hobbies.2 }}</p>

<ul>
    <li>{{ hobbies.0 }}</li>
    <li>{{ hobbies.1 }}</li>
    <li>{{ hobbies.2 }}</li>
</ul>

<p>学校信息:{{ school_info }}</p>
<p>学号:{{ school_info.student_no }}</p>
<p>学校:{{ school_info.school_name }}</p>
<p>班级:{{ school_info.class_name }}</p>

<p>价格: {{ books.性能分析调优.price }}</p>

</body>
</html>

test_jinja2.py

import uvicorn
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates

app = FastAPI()

templates = Jinja2Templates(directory="templates")  # 实例化Jinja2对象,并将文件夹路径设置为以templates命令的文件夹


@app.get("/index")
def index(request: Request):  # TemplateResponse要求上下文包含Request类型的对象,这里需要传入
    name = "韧"
    job = "tester"
    hobbies = ["coding", "swimming", "basketball"]
    school_info = {"student_no": 12345, "class_name": "一年级一班", "school_name": "朝阳小学"}
    books = {
        "性能分析调优": {"price": 100, "publish": "人民教育出版社"},
    }

    return templates.TemplateResponse(
        "index.html",  # 模板文件
        {
            "user": name,
            "job": job,
            "hobbies": hobbies,
            "school_info": school_info,
            "books": books,
            "request": request,  # TemplateResponse要求上下文(context)中必须包含 "request" 键,value是Request类型对象
        },  # context上下文对象,一个字典
    )


if __name__ == '__main__':
    uvicorn.run("test_jinja2:app", port=8001, reload=True)

接口文档请求

image

浏览器请求

image