from django.core.paginator import Paginatorhtml
from django.core.paginator import Paginator def index(request): book_list = Book.objects.all() ''' 批量導入數據: 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() #book表記錄(一行一行數據)的對象 paginator = Paginator(book_list, 10) #每頁顯示10條#Paginator對象 print("count:",paginator.count) #數據總數(總共多少行數據) print("num_pages",paginator.num_pages) #總頁數(總共多少頁) print("page_range",paginator.page_range) #頁碼的列表 page=paginator.page(1) #要顯示第幾頁的數據 for i in page: #遍歷該頁的全部數據對象 print(i) print(page.has_next()) # 下一頁 print(page.has_previous()) # 上一頁 True 或False print(page.next_page_number()) #下一頁的頁碼號 print(page.previous_page_number())#上一頁的頁碼號 ''' return render(request, 'index.html', {'book_list': book_list})
在項目的基礎上:項目--圖書管理系統--第三階段--ajax局部刷新前端
1 {% extends 'base.html' %} 2 3 {% block content %} 4 <div class="col-md-10"> 5 <a class="btn btn-success" href="{% url 'add' %}" role="button" style="margin-top: 20px">添加書籍</a> 6 <table class="table table-striped table-hover table-bordered"> 7 <thead> 8 <tr> 9 <th>編號</th> 10 <th>書籍名稱</th> 11 <th>價格</th> 12 <th>出版日期</th> 13 <th>出版社</th> 14 <th>做者</th> 15 <th>操做</th> 16 </tr> 17 18 </thead> 19 <tbody> 20 21 {# 循環動態顯示信息裏面的數據#} 22 {% for book in current_page %} 23 <tr> 24 {# 顯示編號#} 25 <th>{{ forloop.counter }}</th> 26 <th>{{ book.title }}</th> 27 <th>{{ book.price }}</th> 28 <th>{{ book.pub_date|date:'Y-m-d' }}</th> 29 <th>{{ book.publish }}</th> 30 <th> 31 {# 跨表獲取數據#} 32 {% for author in book.authors.all %} 33 {{ author.name }} 34 {# 顯示多個數據之間用的逗號forloop.last 若是這是最後一次循環,則爲真#} 35 {% if not forloop.last %} 36 , 37 {% endif %} 38 {% endfor %} 39 40 </th> 41 <th> 42 <a class="btn btn-danger btn-sm" href="{% url 'edit' book.pk %}" role="button">編輯</a> 43 <a class="btn btn-warning btn-sm" href="{% url 'delete' book.pk %}" role="button">刪除</a> 44 </th> 45 </tr> 46 {% endfor %} 47 </tbody> 48 </table> 49 50 </div> 51 {% endblock %}
注意:接收後端模板語法變量變了ajax
urls.pydjango
path('index/', views.index,name='index')
views.py後端
from django.core.paginator import Paginator def index(request): book_list = Book.objects.all() paginator = Paginator(book_list, 5) current_page_num=request.GET.get('page',1) #若是取不到數據,就取第一頁的#取值page是多少 current_page=paginator.page(current_page_num)#顯示第幾頁數據 return render(request, 'index.html', {'current_page': current_page})
url不加page,默認第一頁瀏覽器
上面這種對於客戶來講太不友好,想看第幾頁還得在地址欄輸入,那麼有沒有其餘方法呢?服務器
1 {% extends 'base.html' %} 2 3 {% block content %} 4 <div class="col-md-10"> 5 <a class="btn btn-success" href="{% url 'add' %}" role="button" style="margin-top: 20px">添加書籍</a> 6 <table class="table table-striped table-hover table-bordered"> 7 <thead> 8 <tr> 9 <th>編號</th> 10 <th>書籍名稱</th> 11 <th>價格</th> 12 <th>出版日期</th> 13 <th>出版社</th> 14 <th>做者</th> 15 <th>操做</th> 16 </tr> 17 18 </thead> 19 <tbody> 20 21 {# 循環動態顯示信息裏面的數據#} 22 {% for book in current_page %} 23 <tr> 24 {# 顯示編號#} 25 <th>{{ forloop.counter }}</th> 26 <th>{{ book.title }}</th> 27 <th>{{ book.price }}</th> 28 <th>{{ book.pub_date|date:'Y-m-d' }}</th> 29 <th>{{ book.publish }}</th> 30 <th> 31 {# 跨表獲取數據#} 32 {% for author in book.authors.all %} 33 {{ author.name }} 34 {# 顯示多個數據之間用的逗號forloop.last 若是這是最後一次循環,則爲真#} 35 {% if not forloop.last %} 36 , 37 {% endif %} 38 {% endfor %} 39 40 </th> 41 <th> 42 <a class="btn btn-danger btn-sm" href="{% url 'edit' book.pk %}" role="button">編輯</a> 43 <a class="btn btn-warning btn-sm" href="{% url 'delete' book.pk %}" role="button">刪除</a> 44 </th> 45 </tr> 46 {% endfor %} 47 </tbody> 48 </table> 49 50 </div> 51 <nav aria-label="Page navigation"> 52 <ul class="pagination"> 53 54 {% if current_page.has_previous %} 55 <li> 56 <a href="?page={{ current_page.previous_page_number }}" aria-label="Previous"> 57 <span aria-hidden="true">上一頁</span> 58 </a> 59 </li> 60 {% else %} 61 <li class="disabled"><a href="">上一頁</a></li> 62 {% endif %} 63 64 {# 頁碼的顯示#} 65 {% for num in paginator.page_range %} 66 {# 數字 字符串 不能比較#} 67 {% if num == current_page_num %} 68 <li class="active"><a href="?page={{ num }}">{{ num }}</a></li> 69 {% else %} 70 <li><a href="?page={{ num }}">{{ num }}</a></li> 71 {% endif %} 72 73 {% endfor %} 74 75 {% if current_page.has_next %} 76 <li> 77 <a href="?page={{ current_page.next_page_number }}" aria-label="next"> 78 <span aria-hidden="true">下一頁</span> 79 </a> 80 </li> 81 {% else %} 82 <li class="disabled"><a href="">下一頁</a></li> 83 {% endif %} 84 </ul> 85 </nav> 86 {% endblock %}
from django.core.paginator import Paginator,EmptyPage #EmptyPage:一個錯誤類型 def index(request): book_list = Book.objects.all() paginator = Paginator(book_list, 5) try: #若是取超過頁碼的頁面會報錯 current_page_num=request.GET.get('page',1) #若是取不到數據,就取第一頁的#取值page是多少 current_page=paginator.page(current_page_num)#顯示第幾頁數據 except EmptyPage as e: current_page_num=1 current_page = paginator.page(1) return render(request, 'index.html', {'current_page': current_page,'paginator':paginator,'current_page_num':int(current_page_num)})
缺點:若是頁數太多,那就會顯示太多,下一步優化顯示下面的效果app