在django中使用自定義標(biāo)簽實(shí)現(xiàn)分頁(yè)功能
效果演示:
github地址:https://github.com/mncu/django_projects/tree/master/django_projects/pagination_test
本例中總頁(yè)數(shù)為30頁(yè),顯示頁(yè)數(shù)為12頁(yè),當(dāng)前頁(yè)的前排頁(yè)數(shù)為4,后排頁(yè)數(shù)為5
將分頁(yè)分為三種情況:
1 當(dāng)前頁(yè)為第1頁(yè)到第7頁(yè)的時(shí)候,無(wú)省略頁(yè),且12個(gè)位置的內(nèi)容是不變
2 當(dāng)前頁(yè)為第8頁(yè)到第25頁(yè)時(shí),位置1與位置2內(nèi)容不變,當(dāng)前頁(yè)一直處于位置7,
3 當(dāng)前頁(yè)為第25頁(yè)到第30頁(yè)時(shí),位置1與位置2內(nèi)容不變,位置8到位置12的內(nèi)容不變,當(dāng)前頁(yè)在位置8到位置12之中變換
自定義標(biāo)簽代碼:
from django import template register = template.Library() @register.assignment_tag def 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='' # 當(dāng)總頁(yè)數(shù)小于等于 顯示頁(yè)數(shù) 時(shí),則將總頁(yè)數(shù)全部顯示 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
來(lái)看html代碼
{% load mytags %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <!-- 新 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" rel="external nofollow" > <!-- 可選的Bootstrap主題文件(一般不用引入) --> <link rel="stylesheet" rel="external nofollow" > <!-- jQuery文件。務(wù)必在bootstrap.min.js 之前引入 --> <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script> <!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script> </head> <body> {{ current_page.object_list }} <nav> <ul class="pagination"> {% if current_page.has_previous %} <li ><a href="?page={{ current_page.previous_page_number }}" rel="external nofollow" >上一頁(yè) <span class="sr-only">(current)</span></a></li> {% endif %} {% pagination current_page paginator 12 5 as page_list %} <!-- 引用自定義標(biāo)簽,并傳入?yún)?shù) --> {{ page_list|safe }} <!-- 顯示 --> {% if current_page.has_next %} <li><a href="?page={{ current_page.next_page_number }}" rel="external nofollow" >下一頁(yè) <span class="sr-only">(current)</span></a></li> {% endif %} </ul> </nav> <script> $(document).ready(function(){ $('.pagination li a').each(function(){ if ( $(this).html() == {{ current_page.number }} ){ $(this).parent().addClass('active') } }); }) </script> </body> </html>
看看view函數(shù):
from django.shortcuts import render from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger # Create your views here. def index(request): obj_list = ['page01','page02','page03','page04','page05','page06','page07','page08','page09','page10', 'page11','page12','page13','page14','page15','page16','page17','page18','page19','page20', 'page21','page22','page23','page24','page25','page26','page27','page28','page29','page30',] #create a paginator instance paginator = Paginator(obj_list,1) #Get the page_number of current page current_page_num = request.GET.get('page') try: current_page = paginator.page(current_page_num) except PageNotAnInteger: # If page is not an integer, deliver first page. current_page = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. current_page = paginator.page(paginator.num_pages) return render(request,'index.html', {'current_page': current_page, 'paginator': paginator })
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Linux環(huán)境下MySQL-python安裝過(guò)程分享
這篇文章主要介紹了Linux環(huán)境下MySQL-python安裝過(guò)程分享,本文使用的編譯方式安裝,需要的朋友可以參考下2015-02-02Pandas數(shù)據(jù)類型之category的用法
Pandas中有一種特殊的數(shù)據(jù)類型叫做category.它表示的是一個(gè)類別,一般用在統(tǒng)計(jì)分類中,比如性別,血型,分類,級(jí)別等等.有點(diǎn)像java中的enum,今天給大家詳細(xì)講解一下category的用法,需要的朋友可以參考下2021-06-06pytorch 一行代碼查看網(wǎng)絡(luò)參數(shù)總量的實(shí)現(xiàn)
這篇文章主要介紹了pytorch實(shí)現(xiàn)一行代碼查看網(wǎng)絡(luò)參數(shù)總量的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-05-05解決Python 中英文混輸格式對(duì)齊的問(wèn)題
今天小編就為大家分享一篇解決Python 中英文混輸格式對(duì)齊的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07django虛擬環(huán)境(virtualenv)的創(chuàng)建
在使用django開(kāi)發(fā)項(xiàng)目的時(shí)候,一個(gè)環(huán)境只能對(duì)應(yīng)一個(gè)項(xiàng)目,若不安裝虛擬環(huán)境、都裝在系統(tǒng)里面,每次項(xiàng)目加載都需要加載所有的安裝包,本文就介紹django虛擬環(huán)境的安裝,感興趣的可以了解一下2021-08-08python讀出當(dāng)前時(shí)間精度到秒的代碼
在本文里小編給各位分享了一篇關(guān)于python怎么讀出當(dāng)前時(shí)間精度到秒的內(nèi)容,對(duì)此有需要的朋友們可以學(xué)習(xí)參考下。2019-07-07python實(shí)現(xiàn)每天自動(dòng)簽到領(lǐng)積分的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)每天自動(dòng)簽到領(lǐng)積分的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08tensorflow實(shí)現(xiàn)加載mnist數(shù)據(jù)集
這篇文章主要為大家詳細(xì)介紹了tensorflow實(shí)現(xiàn)加載mnist數(shù)據(jù)集,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09