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)

思路:

flask分页
flask分页

最终实现就不贴出来了

相关文章:

  • 2018-06-22
  • 2022-01-02
  • 2021-10-20
  • 2019-01-02
  • 2022-01-01
  • 2021-12-25
  • 2021-06-24
  • 2021-10-19
猜你喜欢
  • 2021-12-04
  • 2019-12-25
  • 2021-07-14
  • 2020-03-11
  • 2021-09-04
  • 2021-05-13
  • 2018-09-29
  • 2018-06-22
相关资源
相似解决方案