分页,主要用于python django框架
生活随笔
收集整理的這篇文章主要介紹了
分页,主要用于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>轉載于:https://www.cnblogs.com/HouZhenglan/p/8480397.html
總結
以上是生活随笔為你收集整理的分页,主要用于python django框架的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 飞在空中的仓库再配合无人机送货,沃尔玛新
- 下一篇: java左移、右移、无符号右移