python通用分页功能

实现:

 1 class Page:
 2 
 3     def __init__(self,current_page,data_count,per_page_count=10,pager_num=10):
 4         self.current_page = current_page
 5         self.data_count = data_count
 6         self.per_page_count = per_page_count
 7         self.pager_num = pager_num
 8 
 9     @property
10     def start(self):
11         return (self.current_page-1) * self.per_page_count
12 
13     @property
14     def end(self):
15         return self.current_page * self.per_page_count
16 
17     @property
18     def total_count(self):
19         total_num,remainder = divmod(self.data_count,self.per_page_count)
20         if remainder:
21             total_num += 1
22         return total_num
23 
24     def page_str(self,base_url):
25         page_list = []
26         max = int((self.pager_num+1)/2)
27         min = int((self.pager_num-1)/2)
28         if self.total_count < self.pager_num:
29             start_index = 1
30             end_index = self.total_count + 1
31         else:
32             if self.current_page <= max:
33                 start_index = 1
34                 end_index = self.pager_num + 1
35             else:
36                 start_index = self.current_page - min
37                 end_index = self.current_page + max
38                 if self.current_page + max > self.total_count:
39                     start_index = self.total_count - self.pager_num + 1
40                     end_index = self.total_count + 1
41 
42         if self.current_page == 1:
43             prev = '<a class="page" href="javascript:void(0);">上一页</a>'
44         else:
45             head = '<a class="page" href="%s?p=%s">首页</a>' % (base_url,1)
46             page_list.append(head)
47             prev = '<a class="page" href="%s?p=%s">上一页</a>' % (base_url,self.current_page - 1)
48         page_list.append(prev)
49 
50         for i in range(start_index, end_index):
51             if i == self.current_page:
52                 temp = '<a class="page active" href="%s?p=%s">%s</a>' % (base_url,i, i)
53             else:
54                 temp = '<a class="page" href="%s?p=%s">%s</a>' % (base_url,i,i)
55             page_list.append(temp)
56 
57         if self.current_page == self.total_count:
58             next = '<a class="page" href="javascript:void(0)">下一页</a>'
59         else:
60             next = '<a class="page" href="%s?p=%s">下一页</a>' % (base_url,self.current_page + 1)
61             tail = '<a class="page" href="%s?p=%s">尾页</a>' % (base_url, self.total_count)
62             page_list.append(tail)
63         page_list.append(next)
64 
65         jump = """
66            <input type='text'/><a onclick='jumpTo(this,"%s?p=");'>GO</a>
67            <script>
68                function jumpTo(ths,base) {
69                    var val = ths.previousSibling.value;
70                    if(val){
71                         location.href = base + val;
72                         }
73                }
74            </script>
75            """%base_url
76 
77         page_list.append(jump)
78         page_str = "".join(page_list)
79         return page_str

 

应用:

def user_list(request):
    current_page = request.GET.get('p', 1)
    current_page = int(current_page)
    page_obj = Page(current_page,len(LIST))
    data =LIST[page_obj.start:page_obj.end]
    page_str = page_obj.page_str('/user_list/')
    return render(request,'user_list.html',{'li':data,'page_str':page_str})

 

posted @ 2019-03-23 14:12  Ray_chen  阅读(1454)  评论(0编辑  收藏  举报