码迷,mamicode.com
首页 > 编程语言 > 详细

python分页的得得

时间:2021-03-10 13:03:32      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:使用   out   ==   count   lse   int   ges   try   turn   

charger_area_list, _ = cls.paginator(charger_area_list, page_no, page_size)

要定义类方法,才可以使用cls调用

class BaseService(object):

    @classmethod #  limit分页
    def limit_data(cls, all_list, page_no, page_size=10):
        limit_data, total_count = limit_page(all_list, page_no, page_size)
        return limit_data, total_count

    @classmethod
    def paginator(cls, data, page_no, page_size=10):
        paginator = Paginator(data, page_size)
        try:
            data_list = paginator.page(page_no)
        except PageNotAnInteger:
            # If page is not an integer, deliver first page.
            data_list = paginator.page(1)
        except EmptyPage:
            # If page is out of range (e.g. 9999), deliver empty list
            data_list = []

        return data_list, paginator.num_pages

limit分页方法

def limit_page(data, page_no=1, page_size=10):
    """分页"""
    list_len = len(data)
    page_no = int(page_no)
    page_size = int(page_size)

    total_page = get_total_page(list_len, page_size)

    if page_no > total_page:
        return None, total_page

    if page_no < total_page:
        return data[(page_no - 1) * page_size:page_no * page_size], total_page

    if page_no == total_page:
        return data[(page_no - 1) * page_size:list_len], total_page
    return data, total_page


def get_total_page(count, page_size):
    return count / page_size if count % page_size == 0 else count / page_size + 1

python分页的得得

标签:使用   out   ==   count   lse   int   ges   try   turn   

原文地址:https://www.cnblogs.com/bkgeng/p/14506789.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!