python-分頁代碼

page.py
'''
django內使用方式:
all_count = models.UserInfo.objects.all().count()
# path_info 當前頁的url
# all_count全部用戶信息的數量
# 10 一頁顯示的數量
page_info = PageInfo(request.GET.get('p'),10,all_count,request.path_info)
print(page_info.end())
user_list = models.UserInfo.objects.all()[page_info.start():page_info.end()]
'''python

class PageInfo(object):
    def __init__(self,current_page,per_page_num,all_count,base_url,page_range=7):
        '''

        :param current_page: 當前頁
        :param per_page_num:  每頁顯示數據的條數
        :param all_count:  數據庫用戶總個數
        :param base_url:  生成頁碼標籤的前綴
        :param page_range:  頁面最多顯示的頁碼個數
        '''
        try:
            current_page = int(current_page)
        except Exception as e:
            current_page = int(1)
        self.current_page = current_page
        self.per_page_num = per_page_num
        self.all_count = all_count
        a,b = divmod(all_count,per_page_num)
        if b != 0:
            self.all_page = a + 1
        else:
            self.all_page = a
        self.base_url = base_url
        self.page_range = page_range

    def start(self):
        return (self.current_page - 1)*self.per_page_num
    def end(self):
        return self.current_page * self.per_page_num
    def page_str(self):
        '''
        在HTML頁面中顯示頁碼信息
        :return:
        '''
        '''
        <nav aria-label="...">
          <ul class="pager">
            <li><a href="#">Previous</a></li>
            <li><a href="#">Next</a></li>
          </ul>
        </nav>
       '''
        page_list = []

        if self.current_page <= 1:
            prev = "<li><a href='#'>上一頁</a></li>"
        else:
            prev = "<li><a href='%s?p=%s'>上一頁</a></li>"%(self.base_url ,self.current_page - 1,)
        page_list.append(prev)

        if self.all_page <=self.page_range:
            start = 1
            end = self.all_page+1
        else:
            # 頁數
            if self.current_page > int(self.page_range/2):
                if (self.current_page + int(self.page_range/2)) > self.all_page:
                    start = self.all_page - self.page_range + 1
                    end = self.all_page + 1
                else:
                    start = self.current_page-int(self.page_range/2)
                    end = self.current_page+int(self.page_range/2)+1
            else:
                start = 1
                end = self.page_range+1

        for i in range(start,end):
            if self.current_page == i:
                temp =  "<li class='active'><a href='%s?p=%s'>%s</a></li>" %(self.base_url,i,i)
            else:
                temp = "<li ><a href='%s?p=%s'>%s</a></li>" % (self.base_url, i, i)
            page_list.append(temp)

        if self.current_page >= self.all_page:
            nex = "<li><a href='#'>下一頁</a></li>"
        else:
            nex = "<li><a href='%s?p=%s'>下一頁</a></li>"%(self.base_url, self.current_page + 1,)
        page_list.append(nex)

        return ''.join(page_list)
相關文章
相關標籤/搜索