网站首页 > 技术文章 正文
- 查询 query 参数(上一篇)
- 路径 path 参数(本篇)
- 请求体 body 参数(下一篇)
- 请求头 header 参数
本篇项目目录结构:
1. 路径参数
路径参数是 URL 地址的一部分,是必填的。路径参数可以是:
- 常规路径参数
- 子路径参数(参数是路径)
- 枚举路径参数
FastAPI 框架同样提供了对路径参数进行条件校验的支持,通过如下图的 Path 实现。
下面一一介绍。
1.1. 常规路径参数
常规路径路由参数格式是使用花括号 {},比如:
/patient/{patient_id}
上面的 {patient_id} 会使用传递的路径中的具体值替换。详细的示例如下:
# 通过 @app.get 装饰器创建一个 API 端点路由
# 常规路径参数
@app.get("/usr/{usr_id}/article/{article_id}", summary='获取用户文章')
async def query_user_article(usr_id: int, article_id: str):
return {
"user_id": usr_id,
"article_id": article_id
}
运行效果如下:
1.2. 子路径参数
路径参数的值本身是路径的情况,需要指定其为 path,语法格式:{参数: path}。示例如下:
# {file_path:path},path 表示该参数是路径
@app.get("/usr/{file_path:path}", summary="获取文件")
async def get_file(file_path: str):
return {
"file_path": file_path
}
运行效果如下:
1.3. 枚举路径参数
通常在参数的值是固定的几个值时,会用枚举,当传入的值不是这些值,FastAPI 会抛出异常。在使用枚举前,前花些时间了解一下 python 中的枚举类型吧,官方文档:
(1) https://docs.python.org/zh-cn/3/library/enum.html
(2) https://docs.python.org/zh-cn/3/howto/enum.html
下面先来定义自己的枚举,这里把 类 和 函数 两种方式都尝试了一下:
from enum import Enum
# 函数的方式
Weekday = Enum('Weekday', ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'])
# 类的方式
class ColorModel(str, Enum):
RED = 'red'
GREEN = 'green'
BLUE = 'blue'
YELLOW = 'yellow'
PURPLE = 'purple'
使用枚举类型作为参数:
from mymodels import ColorModel, Weekday
# 如果传入的值不是这些值,则会抛异常
@app.get("/info/{color}", summary="获取颜色信息")
async def get_color(color: ColorModel):
if color == ColorModel.RED:
msg = f'weekday: {Weekday.Monday.name}, color: {color.name}'
elif color == ColorModel.GREEN:
msg = f'weekday: {Weekday.Tuesday.value}, color: {color.value}'
elif color == ColorModel.BLUE:
msg = f'weekday: {Weekday.Monday.name} = {Weekday.Monday.value}, color: {color.name} = {color.value}'
else:
msg = f'{color} ignored'
return {
"weekday_color": msg
}
运行效果如下:
1.4. 路径参数条件校验
文章一开头已经提到 FastAPI 提供 Path 类的封装来设置 路径参数 的条件校验。下面是具体的示例代码:
from fastapi import FastAPI, Path
# FastAPI 提供对 Path 类的封装来设置 路径参数 的条件校验
# ... 表示必填
@app.get("/data/{data_id}/txt/{txt_id}", summary='获取数据')
async def query_data(data_id: int = Path(..., ge=100, title='数据ID', description='数据唯一编号'),
txt_id: str = Path(..., min_length=1, max_length=30)):
return {
"data_id": data_id,
"txt_id": txt_id
}
运行效果如下:
python 的开发效率还是挺高的,在日常工具、DEMO等工作中发挥了不少的作用,个人觉得确实值得学习的。
猜你喜欢
- 2025-05-09 Python基础教程——列表(一)(python列表编程)
- 2025-05-09 Python学习笔记第一篇(2021年12月14日)——图像的位深度
- 2025-05-09 一文搞懂Python中的import与目录层级
- 2025-05-09 用Python写一个图算法之最短路径算法含注释说明
- 2025-05-09 Introduction to Python Lists 列表介绍
- 2025-05-09 Python 列表(List)详解(python列表讲解)
- 2025-05-09 详解Python 基础知识(python 基础 详细)
- 2025-05-09 python海龟绘图turtle(一):画布和窗体
- 2025-05-09 Python使用bokeh及folium实现地理位置信息的交互可视化
- 2025-05-09 Python高手进阶:深入os.path模块高效处理路径问题
- 261℃Python短文,Python中的嵌套条件语句(六)
- 261℃python笔记:for循环嵌套。end=""的作用,图形打印
- 260℃PythonNet:实现Python与.Net代码相互调用!
- 255℃Python实现字符串小写转大写并写入文件
- 254℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 110℃原来2025是完美的平方年,一起探索六种平方的算吧
- 94℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 87℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)