Django的分頁器paginatorcss
01.views:html
1 from django.shortcuts import render, HttpResponse, redirect 2 from app01.models import Book 3 from django.core.paginator import Paginator, EmptyPage # 引入分頁器 4 5 6 def index(request): 7 ''' 8 批量插入數據 9 book_list = [] 10 for i in range(100): # 插入數據 11 book = Book(name="book_%s" % i,price=i*i) 12 book_list.append(book) # 添加book對象到列表裏 13 14 Book.objects.bulk_create(book_list) # 批量保存數據 15 16 :param request: 17 :return: 18 ''' 19 20 book_list = Book.objects.all() 21 22 # 分頁器 23 24 paginator = Paginator(book_list, 8) # 參數1:數據/參數2:每頁數據條數 25 26 print("count:", paginator.count) # 數據總數 27 print("num_pages:", paginator.num_pages) # 總頁數 28 print("page_range:", paginator.page_range) # 頁碼的列表 29 30 current_page_num = int(request.GET.get("page", 1)) # 動態的拿到第幾頁的數據,默認拿到第一頁 31 print("Num:", current_page_num) 32 33 # 設置分頁樣式,前6也,後5也無變化/中間的樣式:選擇的頁數始終位於分頁器中央 34 if paginator.num_pages > 9: # 當總頁數大於9 35 36 if current_page_num - 5 < 1: # 設置前6頁不變 37 page_range = range(1, 11) # 設置顯示的頁碼列表11個 38 elif current_page_num + 5 > paginator.num_pages: 39 page_range = range(paginator.num_pages - 11, paginator.num_pages + 1) 40 else: 41 page_range = range(current_page_num - 5, current_page_num + 6) 42 else: 43 page_range = paginator.page_range 44 45 try: # 若是分頁器超出頁數或小於頁數,捕獲異常 46 47 48 current_page = paginator.page(current_page_num) # 拿到第幾頁的配置對象 49 50 # page1 = paginator.page(1) # 拿到第1頁的數據的對象 51 # 52 # #顯示某一頁具體數據的兩種方式 53 # print("object_list",page1.object_list) # 拿到第1頁的數據 54 55 for i in current_page: # 迭代拿第一頁的每條數據對象 56 print(i) 57 58 except EmptyPage as e: 59 current_page = paginator.page(1) 60 61 return render(request, 'index.html', locals())
02.index.html:django
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>index</title> 6 <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" 7 integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 8 </head> 9 <body> 10 {#每頁數據內容#} 11 <ul> 12 {% for book in current_page %} 13 <li>{{ book.name }}:{{ book.price }}</li> 14 {% endfor %} 15 </ul> 16 17 {#分頁器#} 18 <nav aria-label="Page navigation"> 19 <ul class="pagination"> 20 {% if current_page.has_previous %}{# 若是分頁器配置對象是否還有上一頁 #} 21 <li> 22 <a href="?page={{ current_page.previous_page_number }}" aria-label="Previous">{# 若是有上一頁,點擊上一頁則會跳轉上一頁的page地址 #} 23 <span aria-hidden="true">«</span> 24 </a> 25 </li> 26 {% else %}{# 若是沒有上一頁,就對上一頁按鈕進行設置class="disabled",隱藏按鈕 #} 27 <li class="disabled"> 28 <a href="" aria-label="Previous">{# 取消跳轉地址 #} 29 <span aria-hidden="true">«</span> 30 </a> 31 </li> 32 {% endif %} 33 34 {% for item in page_range %}{# 循環頁碼列表page_range,拿到每頁頁碼 #} 35 {% if current_page_num == item %}{# 拿到當前頁數 #} 36 <li class="active"><a href="?page={{ item }}">{{ item }}</a></li>{# bootstrap樣式avtive爲選擇器,a標籤跳轉到選擇的頁數 #} 37 {% else %} 38 <li><a href="?page={{ item }}">{{ item }}</a></li>{# 沒有選中的不給active #} 39 {% endif %} 40 {% endfor %} 41 {% if current_page.has_next %}{# 判斷是否有下一個值 #} 42 <li> 43 <a href="?page={{ current_page.next_page_number }}" aria-label="Next"> 44 <span aria-hidden="true">»</span> 45 </a> 46 </li> 47 {% else %} 48 <li class="disabled"> 49 <a href="" aria-label="Next"> 50 <span aria-hidden="true">»</span> 51 </a> 52 </li> 53 {% endif %} 54 55 </ul> 56 </nav> 57 </body> 58 </html>