django -----分頁器組件

分頁器組件

本文目錄

1 Django的分頁器(paginator)簡介

在頁面顯示分頁數據,須要用到Django分頁器組件css

from django.core.paginator import Paginatorhtml

複製代碼
Paginator對象:    paginator = Paginator(user_list, 10) # per_page: 每頁顯示條目數量 # count: 數據總個數 # num_pages:總頁數 # page_range:總頁數的索引範圍,如: (1,10),(1,200) # page: page對象 
page對象:page=paginator.page(1) # has_next 是否有下一頁 # next_page_number 下一頁頁碼 # has_previous 是否有上一頁 # previous_page_number 上一頁頁碼 # object_list 分頁以後的數據列表 # number 當前頁 # paginator paginator對象
複製代碼

 

2 應用View層

複製代碼
from django.shortcuts import render,HttpResponse # Create your views here.
from app01.models import *
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger def index(request): ''' 批量導入數據: Booklist=[] for i in range(100): Booklist.append(Book(title="book"+str(i),price=30+i*i)) Book.objects.bulk_create(Booklist) '''

    ''' 分頁器的使用: book_list=Book.objects.all() paginator = Paginator(book_list, 10) print("count:",paginator.count) #數據總數 print("num_pages",paginator.num_pages) #總頁數 print("page_range",paginator.page_range) #頁碼的列表 page1=paginator.page(1) #第1頁的page對象 for i in page1: #遍歷第1頁的全部數據對象 print(i) print(page1.object_list) #第1頁的全部數據 page2=paginator.page(2) print(page2.has_next()) #是否有下一頁 print(page2.next_page_number()) #下一頁的頁碼 print(page2.has_previous()) #是否有上一頁 print(page2.previous_page_number()) #上一頁的頁碼 # 拋錯 #page=paginator.page(12) # error:EmptyPage #page=paginator.page("z") # error:PageNotAnInteger ''' book_list=Book.objects.all() paginator = Paginator(book_list, 10) page = request.GET.get('page',1) currentPage=int(page) try: print(page) book_list = paginator.page(page) except PageNotAnInteger: book_list = paginator.page(1) except EmptyPage: book_list = paginator.page(paginator.num_pages) return render(request,"index.html",{"book_list":book_list,"paginator":paginator,"currentPage":currentPage})
複製代碼

3 模版層 index.html

複製代碼
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>

<div class="container">

    <h4>分頁器</h4>
    <ul> {% for book in book_list %} <li>{{ book.title }} -----{{ book.price }}</li> {% endfor %} </ul>


    <ul class="pagination" id="pager"> {% if book_list.has_previous %} <li class="previous"><a href="/index/?page={{ book_list.previous_page_number }}">上一頁</a></li> {% else %} <li class="previous disabled"><a href="#">上一頁</a></li> {% endif %} {% for num in paginator.page_range %} {% if num == currentPage %} <li class="item active"><a href="/index/?page={{ num }}">{{ num }}</a></li> {% else %} <li class="item"><a href="/index/?page={{ num }}">{{ num }}</a></li> {% endif %} {% endfor %} {% if book_list.has_next %} <li class="next"><a href="/index/?page={{ book_list.next_page_number }}">下一頁</a></li> {% else %} <li class="next disabled"><a href="#">下一頁</a></li> {% endif %} </ul>
</div>



</body>
</html>
複製代碼

4 擴展

''' 顯示左5,右5,總共11個頁, 1 若是總頁碼大於11 1.1 if 當前頁碼減5小於1,要生成1到12的列表(顧頭不顧尾,共11個頁碼) page_range=range(1,12) 1.2 elif 當前頁碼+5大於總頁碼,生成當前頁碼減10,到當前頁碼加1的列表(顧頭不顧尾,共11個頁碼) page_range=range(paginator.num_pages-10,paginator.num_pages+1) 1.3 else 生成當前頁碼-5,到當前頁碼+6的列表 page_range=range(current_page_num-5,current_page_num+6) 2 其它狀況,生成的列表就是pageinator的page_range page_range=paginator.page_range '''
核心邏輯

 

複製代碼
def index(request): book_list=Book.objects.all() paginator = Paginator(book_list, 15) page = request.GET.get('page',1) currentPage=int(page) # 若是頁數十分多時,換另一種顯示方式
if paginator.num_pages>11: if currentPage-5<1: pageRange=range(1,11) elif currentPage+5>paginator.num_pages: pageRange=range(currentPage-5,paginator.num_pages+1) else: pageRange=range(currentPage-5,currentPage+5) else: pageRange=paginator.page_range try: print(page) book_list = paginator.page(page) except PageNotAnInteger: book_list = paginator.page(1) except EmptyPage: book_list = paginator.page(paginator.num_pages) return render(request,"index.html",locals())
複製代碼

 

 

def page_test(request): # book_list=[]
    # for i in range(100):
    # book=Book(name='book%s'%i,price=10+i,pub_date='2018-09-18',publish_id=1)
    # book_list.append(book)
    # Book.objects.bulk_create(book_list,10)
    book_list=Book.objects.all() # 生成paginator對象,傳入書籍列表,每頁10條數據
    paginator=Paginator(book_list,3) # 總頁碼數
    print(paginator.num_pages) # 頁碼列表
    print(paginator.page_range) # 總數據
    print(paginator.count) # 獲取頁面傳來的頁碼
    current_page=int(request.GET.get('page',1)) page_range=[] # 左5 右5

    # 獲取頁面傳來的頁碼的page對象
    try: page=paginator.page(current_page) # print(page.has_next()) #是否有下一頁
        # print(page.next_page_number()) #下一頁的頁碼
        # print(page.has_previous()) #是否有上一頁
        # print(page.previous_page_number()) #上一頁的頁碼
        # 循環打印出當頁對象
        for i in page: print(i) except Exception as e: current_page=1 page = paginator.page(1) if paginator.num_pages>11: if current_page+5>paginator.num_pages: page_range=range(paginator.num_pages-10,paginator.num_pages+1) elif current_page-5<1: page_range=range(1,12) else: page_range=range(current_page-5,current_page+6) else: page_range=paginator.page_range return render(request,'page_test.html',locals())
views
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.min.css">
    <title>Title</title>
</head>
<body>

<ul> {% for foo in page %} <li>{{ foo.name }}</li> {% endfor %} </ul>
<nav aria-label="Page navigation">
    <ul class="pagination"> {% if page.has_previous %} <li>
                <a href="/page_test/?page={{ page.previous_page_number }}" aria-label="Previous">
                    <span aria-hidden="true">上一頁</span>
                </a>
            </li> {% else %} <li class="disabled">
                <a href="#" aria-label="Previous">
                    <span aria-hidden="true">上一頁</span>
                </a>
            </li> {% endif %} {% for foo in page_range %} {% if current_page == foo %} <li class="active"><a href="/page_test/?page={{ foo }}">{{ foo }}</a></li> {% else %} <li><a href="/page_test/?page={{ foo }}">{{ foo }}</a></li> {% endif %} {% endfor %} {% if page.has_next %} <li>
                <a href="/page_test/?page={{ page.next_page_number }}" aria-label="Next">
                    <span aria-hidden="true">下一頁</span>
                </a>
            </li> {% else %} <li class="disabled">
                <a href="#" aria-label="Next">
                    <span aria-hidden="true">下一頁</span>
                </a>
            </li> {% endif %} </ul>
</nav>


</body>
</html>
模版

 ---------https://www.cnblogs.com/liuqingzheng/articles/9509767.htmldjango

相關文章
相關標籤/搜索