Django-自定义分页2

views


class PageInfo(object):
    def __init__(self, cur_page,all_count, per_page,base_url, show_page=11):
        """

        :param cur_page: 当前页码
        :param all_count: 数据库总行数
        :param per_page: 每页显示行数
        """
        try:
            self.cur_page = int(cur_page)
        except Exception as e:
            self.cur_page = 1
        self.per_page = per_page
        div, mod = divmod(all_count, per_page)
        if mod:
            div += 1
        self.all_pager = div
        self.base_url = base_url
        self.show_page = show_page

    def start(self):
        return (self.cur_page - 1) * self.per_page

    def end(self):
        return self.cur_page * self.per_page

    def pager(self):
        # v = '1'
        # return v
        page_list = []



        # 展示11条,前5后5
        half = self.show_page//2

        # 如果总数据小于11页,展示所有页码
        if self.all_pager <= self.show_page:
            begin =1
            stop = self.all_pager + 1

        else:
            # 如果当前页<=5,用于显示前面11条
            if self.cur_page <= half:
                begin =1
                stop = self.show_page + 1

            # 如果当前页是末页前5页,那展示末页前11条
            elif self.cur_page + half >= self.all_pager:
                begin = self.all_pager - self.show_page + 1
                stop = self.all_pager + 1

            # 中间页码
            else:
                begin = self.cur_page - half
                stop = self.cur_page + half + 1


        # 上一页按钮
        if self.cur_page >1:
            pre ='上一页' % (self.base_url,self.cur_page-1)
        else:
            pre ='上一页'

        page_list.append(pre)


        # 中间页码按钮
        for i in range(begin,stop):

            # 当前页码选中效果
            if i == self.cur_page:
                tmp = '%s' % (self.base_url,i, i)
            else:
                tmp = '%s' %(self.base_url,i,i)
            page_list.append(tmp)

        # 下一页按钮
        if self.cur_page 

template




    
    Title


    
    {% for user in user_list %}
  • {{ user.user }}
  • {% endfor %}
{{ page_info.pager|safe }}

你可能感兴趣的:(Django)