Django自定製分頁功能
阿新 • • 發佈:2018-11-25
URL:
"""django_paginner URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: https://docs.djangoproject.com/en/2.1/topics/http/urls/ Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path from app01 import views urlpatterns = [ path('admin/', admin.site.urls), path('index/', views.index), path('index1/', views.index1), ]
views:
from django.shortcuts import render, HttpResponse from django.core.paginator importPaginator, EmptyPage, PageNotAnInteger # Create your views here. USER_LIST = [] for i in range(1, 659): temp = {'name': 'root' + str(i), 'age': i} USER_LIST.append(temp) def index(request): # 當前的頁數 current_page = request.GET.get('p') # 每頁顯示10個 per_page_count = 10 # 第一頁(0,10)索引是0到9 # 第二頁(10,20)索引是10到19 current_page = int(current_page) # 開始:當前頁-1*每頁顯示10個 # 第二頁 # (2-1)*10 start = (current_page - 1) * per_page_count # 2*10 end = current_page * per_page_count data = USER_LIST[start:end] if current_page <= 1: prev_page = 1 prev_page = current_page - 1 next_page = current_page + 1 return render(request, 'index.html', {'user_list': data, 'prev_page': prev_page, 'next_page': next_page}) class CustomPaginator(Paginator): def __init__(self, current_page, per_pager_num, *args, **kwargs): # 當前頁 self.current_page = int(current_page) # 每頁最多顯示頁碼數量 self.per_page_num = int(per_pager_num) super(CustomPaginator, self).__init__(*args, **kwargs) def pager_num_range(self): # 當前頁 # self.current_page # 每頁最多能顯示的頁碼數量,漂亮最好取奇數 11 # self.per_page_num # 總頁數 # self.num_pages # 總頁數是django分頁功能根據資料大小自動分配好的 # 如果總頁數小於每頁最多能顯示的頁碼數量 # 那麼就返回range(第一頁到總頁數+1),因為range取值是取(開頭,結尾-1) if self.num_pages < self.per_page_num: return range(1, self.num_pages + 1) # 總頁數特別多 # 當你的當前頁 小於或等於,最多顯示的頁數的一半時,顯示前五個,後五個 part = int(self.per_page_num / 2) if self.current_page <= part: # return range(1, part + 1) return range(1, self.per_page_num + 1) # 極限值:如果當前頁+最多顯示頁數一半>總頁數(超出了顯示的範圍) # 返回range(總頁數-最多顯示的頁數,總頁數+1) if (self.current_page + part) > self.num_pages: return range(self.num_pages-self.per_page_num, self.num_pages + 1) # if self.current_page > part: return range(self.current_page - part, self.current_page + part + 1) def index1(request): # 全部資料:USER_LIST=得出共有多少條資料 # 10, # per_page: 每頁顯示條目數量 # count: 資料總個數 # num_pages:總頁數 # page_range:總頁數的索引範圍,如: (1,10),(1,200) # page: page物件(是否具有下一頁;是否有上一頁) current_page = request.GET.get('p') paginator = CustomPaginator(current_page, 7, USER_LIST, 10) try: posts = paginator.page(current_page) # has_next 是否有下一頁 # next_page_number 下一頁頁碼 # has_previous 是否有上一頁 # previous_page_number 上一頁頁碼 # object_list 分頁之後的資料列表 # number 當前頁 # paginator paginator物件 except PageNotAnInteger: posts = paginator.page(1) except EmptyPage: posts = paginator.page(paginator.num_pages) return render(request, 'index1.html', {'posts': posts})
templates-index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <ul> {% for row in posts.object_list %} <li>{{ row.name }}-{{ row.age }}</li> {% endfor %} </ul> {% include 'include/pager.html' %} </body> </html>
pager.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% if posts.has_previous %} <a href="/index1?p={{ posts.previous_page_number }}">上一頁</a> {% else %} <a href="#">上一頁</a> {% endif %} {% for i in posts.paginator.pager_num_range %} {% if i == posts.number %} <a style="font-size: 30px;" href="/index1?p={{ i }}">{{ i }}</a> {% else %} <a href="/index1?p={{ i }}">{{ i }}</a> {% endif %} {% endfor %} {% if posts.has_next %} <a href="/index1/?p={{ posts.next_page_number }}">下一頁</a> {% else %} <a href="#">下一頁</a> {% endif %} <span> {{ posts.number }}/{{ posts.paginator.num_pages }} </span> </body> </html>