国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

在django中使用自定義標簽實現分頁功能

2020-02-16 01:47:59
字體:
來源:轉載
供稿:網友

效果演示:

 

github地址:https://github.com/mncu/django_projects/tree/master/django_projects/pagination_test

本例中總頁數為30頁,顯示頁數為12頁,當前頁的前排頁數為4,后排頁數為5

將分頁分為三種情況:

1   當前頁為第1頁到第7頁的時候,無省略頁,且12個位置的內容是不變

2  當前頁為第8頁到第25頁時,位置1與位置2內容不變,當前頁一直處于位置7,

3  當前頁為第25頁到第30頁時,位置1與位置2內容不變,位置8到位置12的內容不變,當前頁在位置8到位置12之中變換

自定義標簽代碼:

from django import templateregister = template.Library()@register.assignment_tagdef pagination(current_page,paginator,num_of_displaypages=10,num_of_backpages=4): # current_page is a django.core.paginator.Page 's instance # paginator is a django.core.paginator.Paginator 's instance # num_of_frontpages = num_of_displaypages - num_of_backpages -3 html='' # 當總頁數小于等于 顯示頁數 時,則將總頁數全部顯示 if paginator.num_pages <= num_of_displaypages :  for i in range(1,paginator.num_pages+1):   html+= '<li ><a href="?page=%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >%s </a></li>'%(i,i)  return html # 第一種情況  elif current_page.number <= num_of_displaypages-num_of_backpages:  for i in range(1,num_of_displaypages+1):   html+= '<li ><a href="?page=%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >%s </a></li>'%(i,i)  return html # 第二種情況 elif num_of_displaypages-num_of_frontpages <= current_page.number <= paginator.num_pages-num_of_backpages :  html = '''   <li><a href="?page=1" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >1</a></la>   <li class="disabled"><a href="?page=1" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >...</a></la>  '''  for i in range(current_page.number-num_of_frontpages,current_page.number+num_of_backpages+1):   html+='<li><a href="?page=%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >%s</a></la>'%(i,i)  return html # 第三種情況 else:  html = '''   <li><a href="?page=1" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >1</a></la>   <li class="disabled"><a href="?page=1" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >...</a></la>  '''  for i in range(paginator.num_pages-num_of_backpages-num_of_frontpages,paginator.num_pages+1):   html+='<li><a href="?page=%s" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >%s</a></la>'%(i,i)  return html            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天台县| 长乐市| 土默特左旗| 巫溪县| 普兰店市| 兴城市| 湖南省| 财经| 万安县| 蒙山县| 罗江县| 大关县| 拉孜县| 玉溪市| 罗田县| 永福县| 莱州市| 和田县| 西丰县| 武定县| 慈溪市| 衡阳市| 延边| 台南县| 阳泉市| 始兴县| 西青区| 建德市| 玉林市| 固始县| 邵阳县| 汤原县| 循化| 泉州市| 诸暨市| 黔西县| 淮北市| 铜山县| 南安市| 澳门| 沧州市|