日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

分页,主要用于python django框架

發(fā)布時(shí)間:2025/3/21 python 58 豆豆
生活随笔 收集整理的這篇文章主要介紹了 分页,主要用于python django框架 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1 class Page: 2 def __init__(self,list_count,current_page,page_list_num=10,page_num=7): 3 #總條數(shù) 4 self.list_count = list_count 5 #每頁(yè)顯示的數(shù)據(jù)條數(shù) 6 self.page_list_num = page_list_num 7 #顯示的頁(yè)碼 8 self.page_num = page_num 9 #當(dāng)前頁(yè)數(shù) 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 """開(kāi)始條數(shù)""" 28 return int((self.current_page-1)*self.page_list_num) 29 30 @property 31 def end(self): 32 """結(jié)束條數(shù)""" 33 return int(self.start + self.page_list_num + 1) 34 35 @property 36 def start_page(self): 37 """開(kāi)始頁(yè)數(shù)""" 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 """結(jié)束頁(yè)數(shù)""" 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,'上一頁(yè)')) 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,'下一頁(yè)')) 85 86 return self.page_html View Code

主要用于django web框架

用法示例:

1 import page 2 def index(request): 3 #獲取當(dāng)前頁(yè)數(shù) 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>

轉(zhuǎn)載于:https://www.cnblogs.com/HouZhenglan/p/8480397.html

總結(jié)

以上是生活随笔為你收集整理的分页,主要用于python django框架的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。