分页,主要用于python django框架

 1 class Page:
 2     def __init__(self,list_count,current_page,page_list_num=10,page_num=7):
 3         #总条数
 4         self.list_count = list_count
 5         #每页显示的数据条数
 6         self.page_list_num = page_list_num
 7         #显示的页码
 8         self.page_num = page_num
 9         #当前页数
10         if current_page<1:
11             self.current_page = 1
12         elif current_page>self.page_count:
13             self.current_page = self.page_count
14         else:
15             self.current_page = current_page
16     
17 
18     @property
19     def page_count(self):
20         p,x = divmod(self.list_count,self.page_list_num)
21         if x:
22             return p+1
23         else:
24             return p
25     @property
26     def start(self):
27         """开始条数"""
28         return int((self.current_page-1)*self.page_list_num)
29         
30     @property
31     def end(self):
32         """结束条数"""
33         return int(self.start + self.page_list_num + 1)
34         
35     @property
36     def start_page(self):
37         """开始页数"""
38         if self.current_page - (self.page_num-1)/2 < 1:
39             return 1
40         else:
41             return int(self.current_page - (self.page_num-1)/2)
42             
43     @property
44     def end_page(self):
45         """结束页数"""
46         if self.current_page + (self.page_num-1)/2 > self.page_count:
47             return self.page_count
48         else:
49             return int(self.current_page + (self.page_num-1)/2)
50     
51     def page_str(self,url):
52         self.page_html = []
53         if self.page_count <= self.page_num:
54             for i in range(1,self.page_count+1):
55                 if i == self.current_page:
56                     a_tag = '<a class="active" href="%s?p=%s">%s</a>' % (url,i,i)
57                     self.page_html.append(a_tag)
58                 else:
59                     a_tag = '<a href="%s?p=%s">%s</a>' % (url,i,i)
60                     self.page_html.append(a_tag)
61         else:
62             for i in range(self.start_page,self.end_page+1):
63                 if i == self.current_page:
64                     a_tag = '<a class="active" href="%s?p=%s">%s</a>' % (url,i,i)
65                     self.page_html.append(a_tag)
66                 else:
67                     a_tag = '<a href="%s?p=%s">%s</a>' % (url,i,i)
68                     self.page_html.append(a_tag)
69 
70             if self.start_page > 2:
71                 self.page_html.insert(0,'<a>...</a>')
72             if self.start_page - 1 >= 1:
73                 self.page_html.insert(0,'<a href="%s?p=%s">%s</a>' % (url,1,1))
74 
75             
76             if self.end_page < self.page_count - 2:
77                 self.page_html.append('<a>...</a>')
78             if self.end_page + 1 == self.page_count -1:
79                 self.page_html.append('<a href="%s?p=%s">%s</a>' % (url,self.page_count-1,self.page_count-1))
80             if self.end_page <= self.page_count - 1:
81                 self.page_html.append('<a href="%s?p=%s">%s</a>' % (url,self.page_count,self.page_count))
82 
83         self.page_html.insert(0,'<a href="%s?p=%s">%s</a>' % (url,1 if self.current_page==1 else self.current_page-1,'上一页'))
84         self.page_html.append('<a href="%s?p=%s">%s</a>' % (url,self.page_count if self.current_page==self.page_count else self.current_page+1,'下一页'))
85 
86         return self.page_html
View Code

主要用于django web框架

用法示例:

 1 import page
 2 def index(request):
 3     #获取当前页数
 4     p = request.GET.get('p',1)
 5     p = int(p)
 6     objall = models.obj.objects.all()
 7     pg = page.Page(len(objall),p)
 8 
 9     page_number_list = objall[pg.start:pg.end]
10 
11     page_html = pg.page_str("/index/")
12     page_html = "".join(page_html)
13     return render_to_response('index.html',{'page_number_list':page_number_list,'page_html':page_html}
View Code

模板:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>fenye</title>
    <style type="text/css">
        a{
            padding: 5px;
            margin: 5px;
            background-color:#ffffff;
            color: #000000;
            border: 1px solid #000;
            text-decoration: none;
        }
        a.active{
            background-color: red;
        }
    </style>
</head>
<body>
    <ul>
        {% for i in page_number_list %}
            <li>{{i}}</li>
        {% endfor%}  <br>
        <div>{{ page_html|safe }}</div>
    </ul>
</body>
</html>

posted @ 2018-03-01 20:10  kosa  阅读(262)  评论(0编辑  收藏  举报