一.分頁器:css
知識點先覽html
Paginator:Django分頁器
bulk_create:批量插入數據
paginator.count:數據總數,其中paginator是分頁器實例化對象
paginator.num_pages:總頁數
paginator.page_range:頁碼的列表
object_list:分頁以後的數據列表
has_next:是否有下一頁
next_page_number:下一頁頁碼
has_previous:是否有上一頁
previous_page_number:上一頁頁碼
models.pygit
from django.db import models # Create your models here. class Book(models.Model): title = models.CharField(max_length=32) price = models.DecimalField(max_digits=8,decimal_places=2)
views.py數據庫
# -*- encoding:utf-8 -*- from django.shortcuts import render # Create your views here. from app01.models import Book from django.core.paginator import Paginator,EmptyPage # 導入分頁器 def index(request): # 方法很差,每次都要插入數據,須要插入100次 # for i in range(100): # Book.objects.create(title="book_%s"%i,price=i*i) # 批量插入,導入到數據庫裏 bulk_create ''' book_list = [] for i in range(100): book_obj = Book(title="book_%s"%i,price=i*i) book_list.append(book_obj) Book.objects.bulk_create(book_list) ''' book_list = Book.objects.all() # 拿到全部書籍數據 # 分頁器 paginator = Paginator(book_list,8) # 第一個參數是須要分頁的列表,第二個是每頁須要展現的數目 print(paginator.count) # 數據總數 print(paginator.num_pages) # 總頁數 print(paginator.page_range) # 頁碼的列表,總頁數+1 # 100 # 13 # range(1, 14) page1 = paginator.page(1) # 第一頁的page對象,固定死了 # 動態顯示 try: current_page_num = int(request.GET.get("page", 1)) # get數據是經過?鏈接的 http://127.0.0.1:8000/index/?page=2 #作成一個動態的效果。給它個默認值拿第一頁,不加1就會返回一個None。當前想要的第幾頁的數據 current_page = paginator.page(current_page_num) # 拿到第幾頁的page對象 # 顯示每一頁具體數據的兩種方式 print("object_list",page1.object_list) # object_list < QuerySet[ < Book: Book object (1) >, < Book: Book object (2) >, < Book: Book object (3) >, # < Book: Book object (4) >, < Book: Book object (5) >, < Book: Book object (6) >, < Book: Book object (7) >, # < Book: Book object (8) >] > for i in page1: print(i) # Book object (1) # Book object (2) # Book object (3) # Book object (4) # Book object (5) # Book object (6) # Book object (7) # Book object (8) except EmptyPage as e: current_page = paginator.page(1) return render(request,"index.html",locals())
index.htmlnpm
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body> <p3>index</p3> {# 顯示當前頁面的書籍信息 #} <ul> {% for book in current_page %} <li>{{ book.title }}:{{ book.price }}</li> {% endfor %} </ul> <nav aria-label="Page navigation"> // 分頁的頁面 <ul class="pagination"> {# 沒法顯示上一頁和下一頁 #} {# <li>#} {# <a href="?page={{ current_page_num|add:-1 }}" aria-label="Previous">#} // 上一頁:當前頁碼減一 --> href="?page={{current_page_num|add:-1}}" {# <span aria-hidden="true">上一頁</span>#} {# </a>#} {# </li>#} {% if current_page.has_previous %} // 判斷當前頁面是否還有上一頁 <li> <a href="?page={{ current_page.previous_page_number }}" aria-label="Previous"> // 得到當前頁面上一頁的頁碼 <span aria-hidden="true">上一頁</span> </a> </li> {% else %} <li class="disabled"> {# bootstrap設置disabled 不能點擊#} // 上一頁沒數據就不能點擊 <a href="#" aria-label="Previous"> <span aria-hidden="true">上一頁</span> </a> </li> {% endif %} {% for item in paginator.page_range %} // item爲數字,這裏不該該寫死 --> page_range {#{% for item in page_range %}#} {% if current_page_num == item %} {# 判斷當前頁碼是否和循環的頁碼一致 bootstrap設置的active會顯示深色, #} <li class="active"><a href="?page={{ item }}">{{ item }}</a></li> {% else %} <li><a href="?page={{ item }}">{{ item }}</a></li> {% endif %} {% endfor %} {# <li>#} {# <a href="?page={{ current_page_num|add:1 }}" aria-label="Next">#} {# <span aria-hidden="true">下一頁</span>#} {# </a>#} {# </li>#} {% if current_page.has_next %} // 判斷當前頁面是否還有下一頁 <li> <a href="?page={{ current_page.next_page_number }}" aria-label="Next"> // 得到下一頁頁面的頁碼 <span aria-hidden="true">下一頁</span> </a> </li> {% else %} <li class="disabled"> <a href="#" aria-label="Previous"> <span aria-hidden="true">下一頁</span> </a> </li> {% endif %} </ul> </nav> </body> </html>
改進版:django
views.pybootstrap
# -*- encoding:utf-8 -*- from django.shortcuts import render # Create your views here. from app01.models import Book from django.core.paginator import Paginator,EmptyPage # 導入分頁器 def index(request): # 方法很差,每次都要插入數據,須要插入100次 # for i in range(100): # Book.objects.create(title="book_%s"%i,price=i*i) # 批量插入 bulk_create ''' book_list = [] for i in range(100): book_obj = Book(title="book_%s"%i,price=i*i) book_list.append(book_obj) Book.objects.bulk_create(book_list) ''' book_list = Book.objects.all() # 分頁器 paginator = Paginator(book_list,3) # 第一個參數是須要分頁的列表,第二個是每頁須要展現的數目 print(paginator.count) # 數據總數 print(paginator.num_pages) # 總頁數 print(paginator.page_range) # 頁碼的列表,總頁數+1 # 100 # 13 # range(1, 14) page1 = paginator.page(1) # 第一頁的page對象,固定死了 # 動態顯示 current_page_num = int(request.GET.get("page", 1)) # get數據是經過?鏈接的 http://127.0.0.1:8000/index/?page=2 # 固定頁碼數居中 if paginator.num_pages > 11: if current_page_num - 5 < 1: ## 點1,2,3,4,5,6頁碼不動,點7開始左五右五,點倒數五個頁面不動 page_range = range(1,12) elif current_page_num + 6 > paginator.num_pages: page_range = range(paginator.num_pages-10,paginator.num_pages+1) else: page_range = range(current_page_num-5,current_page_num+6) else: page_range = paginator.page_range try: current_page = paginator.page(current_page_num) # 顯示每一頁具體數據的兩種方式 print("object_list",page1.object_list) # object_list < QuerySet[ < Book: Book object (1) >, < Book: Book object (2) >, < Book: Book object (3) >, # < Book: Book object (4) >, < Book: Book object (5) >, < Book: Book object (6) >, < Book: Book object (7) >, # < Book: Book object (8) >] > for i in page1: print(i) # Book object (1) # Book object (2) # Book object (3) # Book object (4) # Book object (5) # Book object (6) # Book object (7) # Book object (8) except EmptyPage as e: current_page = paginator.page(1) return render(request,"index.html",locals())
index,htmlapp
{% for item in page_range %} 不該該寫死,其餘同上 {% if current_page_num == item %} <li class="active"><a href="?page={{ item }}">{{ item }}</a></li> {% else %} <li><a href="?page={{ item }}">{{ item }}</a></li> {% endif %} {% endfor %}