from fastapi import FastAPI
BOOKS = [
{"id": 1, "title": "book1"},
{"id": 2, "title": "book2"},
{"id": 3, "title": "book3"},
{"id": 4, "title": "book4"},
{"id": 5, "title": "book5"},
{"id": 6, "title": "book6"},
{"id": 7, "title": "book7"},
{"id": 8, "title": "book8"}
]
app = FastAPI()
# 查询参数
# /books?page=1&size=10 第1页 第10行
# ?后面为查询的参数对多个参数使用&连接
@app.get("/books/")
def books(page: int, size: int):
# 列表页分页效果 每页显示size个数量
return BOOKS[(page - 1) * size:page * size]
http://10.105.212.1:8000/books/?page=2&size=3
[
{
"id": 4,
"title": "book4"
},
{
"id": 5,
"title": "book5"
},
{
"id": 6,
"title": "book6"
}
]
标签:分页,04,title,FastAPI,page,books,id,size
From: https://www.cnblogs.com/leoshi/p/17242361.html