1. 程式人生 > >Django 第七天:自己寫分頁

Django 第七天:自己寫分頁

1.分頁操作跟隨大神視訊逐步講解自己寫出來。
大致思路:
1.定義好 當前頁,每頁顯示行數,資料總行數,一共顯示的頁數
2.建立page_list列表 首頁和最後一頁分別加入到列表的最前和最後。
3.用if判斷語句來巢狀判定各種條件。
4.重要的是前後的判斷,如當前顯示的頁數小於顯示頁數的一半等。
以下是程式碼,貼上以備不時之需。

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

        :param current_page: 當前頁
        :param all_count: 資料總行數
        :param per_page: 每頁顯示資料數
        :param show_page: 一共顯示的頁數
        """
        try:
            self.current_page=int(current_page)

        except Exception as e:
            self.current_page=1
            ##如果當頁碼不為字串則當前頁碼返回第一頁
        self.per_page=per_page
        a,b=divmod(all_count,per_page)
        ##用divmod計算一共多少頁面
        if b:
            a=a+1
        self.all_page=a
        self.show_page=show_page

    def strat(self):
        return (self.current_page-1)*self.per_page
    def end(self):
        return self.current_page*self.per_page

    def pager(self):
        #v='<a href=/app01/customs.html?page=19>19</a>'
        #以字串形式傳入前端   不過需要加|safe
        half=int((self.show_page-1)/2)
        ##用總的顯示頁面數量的一半表示前後各有多少個,為了前後數量一致總數為單數。
        if self.all_page<self.show_page:

            begin=1
            stop=self.all_page
            ##如果資料總數小於顯示頁面數 永遠從第一頁開始,總頁數為最大頁數
        else:
            if self.current_page<half:
                begin=1
                stop=self.show_page+1
                ##當前頁小於總頁數一半
            else:
                if self.current_page+half>self.all_page:
                    begin=self.all_page-self.show_page+1
                    stop=self.all_page+1

                else:
                    begin =self.current_page-half
                    stop = self.current_page+half+1
        # begin=self.current_page-half
        # #起始位置
        # stop=self.current_page+half+1
        # #末尾位置
        page_list=[]
        frist='<a style ="display:inline-block;margin:3px;padding:3px"href=/app01/customs.html?page=1>首頁</a>'
        page_list.append(frist)
        if self.current_page <=1:
            prev = '<a style ="display:inline-block;margin:3px;padding:3px"href=/app01/customs.html?page=#>上一頁</a>'
        else:
            prev='<a style ="display:inline-block;margin:3px;padding:3px"href=/app01/customs.html?page=%s>上一頁</a>' % (self.current_page-1)
        page_list.append(prev)
        ##建立一個列表來列出所有的頁數
        for i in range(begin,stop):
            ##迴圈所有的頁面
            if i == self.current_page:
                temp = '<a style ="display:inline-block;background-color:red;margin:3px;padding:3px"href=/app01/customs.html?page=%s>%s</a>' % (
                self.current_page, self.current_page)
                ##判斷是否為當前頁,如果為當前頁就顯示為底色為紅色
            else:
                temp ='<a style ="display:inline-block;margin:3px;padding:3px"href=/app01/customs.html?page=%s>%s</a>'%(i,i)
            #調整了一下樣式
            page_list.append(temp)
        if self.current_page >=self.all_page:
            nex = '<a style ="display:inline-block;margin:3px;padding:3px"href=/app01/customs.html?page=#>下一頁</a>'
        else:
            nex = '<a style ="display:inline-block;margin:3px;padding:3px"href=/app01/customs.html?page=%s>下一頁</a>' % (
                        self.current_page + 1)
        page_list.append(nex)
        last=prev = '<a style ="display:inline-block;margin:3px;padding:3px"href=/app01/customs.html?page=%s>尾頁</a>'%(self.all_page)
        page_list.append(last)
        return ''.join(page_list)
        ##用.join 方法temp字串拼接起來