flask分页

xiaoxiao2025-07-16  6

flask+mongoengine实现分页,

前端给第几页和每页条数,后端显示相应数据其中@catch_exception是try except容错装饰器,trueReturn方法为响应方法

分页

class Paging(Resource): """分页""" @catch_exception def post(self): import math data = request.json # 每页多少条 num = int(data["num"]) # 第几页 page = int(data["page"]) # 总数据条数 total = User.objects.all().count() # 共多少页 pages = math.ceil(total / num) # 起始条数 start = num*(page-1) end = num*page # 最终数据 all = User.objects[start:end] list = [] if all: for i in all: obj = { "id": str(i.id), "user_ip": i.user_ip, "timeout_name": i.user_name, "country": i.country, "timeout": i.stay_time, "router": i.router, "create_time": i.create_time } list.append(obj) # 返回结果 result = { "data": list, "total": total, "pages": pages } return trueReturn(result)

思路:

最终实现就不贴出来了

转载请注明原文地址: https://www.6miu.com/read-5033158.html

最新回复(0)