Django 學習組件分頁器與自定製分頁器

 

一.Django 分頁器

1.django的分頁器基礎版

(1)首先是基礎數據分別爲

from django.db import models


# Create your models here.
class Book(models.Model):
    title = models.CharField(max_length=32)
    price = models.DecimalField(max_digits=5, decimal_places=2)
models.py
from django.contrib import admin
from django.urls import path
from app01 import views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('book/', views.Book.as_view())
]
urls.py

(2)基礎版的視圖函數

from django.shortcuts import render, redirect
from django.views import View                 # 導入view
from app01 import models                      # 導入模型
from django.core.paginator import Paginator   # 導入django分頁器
import random


# Create your views here.
class Book(View):
    def get(self, request):
        # 法二:批量增長測試數據  效率塊
        # book_list = []
        # for i in range(1, 101):
        #     # models.Book.objects.create(title="book_%s" % i, price=random.randint(20, 300))  # 法一:執行效率低
        #     book_list.append(models.Book(title="book_%s" % i, price=random.randint(20, 300)))
        # models.Book.objects.bulk_create(book_list)  # 批量新增數據
        # 分頁器的基本語法
        # book_list = models.Book.objects.all()
        # print(book_list)  # QuerySet book對象
        # # 實例化分頁對象
        # paginator = Paginator(book_list, 10)
        # print("count:", paginator.count)               # 數據的總數
        # print("num_pages:", paginator.num_pages)       # 分頁的總頁數
        # print("page_range:",paginator.page_range)     # 頁數的範圍列表
        #
        # page1 = paginator.get_page(1)                  # 獲取第一頁的全部數據
        # for i in page1:                                # 遍歷第一頁的全部數據對象
        #     print(i)
        # print(page1.object_list)                       # 第一頁的所用數據
        # page2 = paginator.get_page(2)                  # 獲取第二頁的全部數據
        # print(page2.has_next())                        # 是否有下一頁
        # print(page2.next_page_number())                # 下一頁的頁碼
        # print(page2.has_previous())                    # 是否有上一頁
        # print(page2.previous_page_number())            # 上一頁的頁碼
        # 當get不存在的頁數或者填寫其餘類型的數據時會報錯
        # page=paginator.page(22)                       # error:EmptyPage
        # page=paginator.page("z")                      # error:PageNotAnInteger


        # ############# django分頁器的基本使用 ##################
        book_list = models.Book.objects.all()
        paginator = Paginator(book_list, 10)            # 每頁分的數量(數據量)

        current_page = int(
            request.GET.get("page") if request.GET.get("page") and request.GET.get("page").isdigit() else 1)  # 獲取頁碼
        page = paginator.get_page(current_page)  # 獲取當前頁碼的全部數據

        return render(request, "book.html", {"paginator": paginator, "page": page, "current_page": current_page})

    def post(self, request):
        return redirect("/book/")
views.py

(3)最後是模板內容

<!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>
<h2>圖書列表</h2>
<ul>
    {% for book in page %}
        <li>{{ book.title }} --- {{ book.price }}</li>
    {% endfor %}
</ul>
<nav aria-label="Page navigation">
    <ul class="pagination">
        <li><a href="?page=1">首頁</a></li>
        {% if page.has_previous %}
            <li><a href="?page={{ page.previous_page_number }}">上一頁</a></li>
        {% else %}
            <li class="disabled"><a href="javascript:void(0);">上一頁</a></li>
        {% endif %}
        {% for num in paginator.page_range %}
            {% if current_page == num %}
                <li class="active"><a href="?page={{ num }}">{{ num }}</a></li>
            {% else %}
                <li><a href="?page={{ num }}">{{ num }}</a></li>
            {% endif %}
        {% endfor %}
        {% if page.has_next %}
            <li><a href="?page={{ page.next_page_number }}">下一頁</a></li>
        {% else %}
            <li class="disabled"><a href="javascript:void(0);">下一頁</a></li>
        {% endif %}
        <li><a href="?page={{ paginator.num_pages }}">尾頁</a></li>
    </ul>
</nav>


</body>
</html>
book.html

2.進階版分頁器

上面的示例,看似已經完成了分頁的效果,可是,若是咱們把每頁顯示的數量改小一點看一下效果。javascript

顯然這樣不是咱們想要的css

(1)進階版視圖函數

from django.shortcuts import render, redirect
from django.views import View  # 導入view
from app01 import models  # 導入模型
from django.core.paginator import Paginator  # 導入django分頁器
import random


# Create your views here.
class Book(View):
    def get(self, request):
        # ############# django分頁器的基本使用進階版 ##################
        # 分頁器的基本語法
        book_list = models.Book.objects.all()

        # 示例分頁器對象
        paginator = Paginator(book_list, 2)      # 每頁顯示數據的數量
        current_page = int(
            request.GET.get("page") if request.GET.get("page") and request.GET.get("page").isdigit() else 1)
        # 獲取頁碼
        page = paginator.get_page(current_page)  # 獲取當前頁碼的全部數據
        # 默認按照11個頁碼展現

        if paginator.num_pages > 11:
            if current_page - 5 < 1:
                page_range = range(1, 12)
            elif current_page + 5 > paginator.num_pages:
                page_range = range(paginator.num_pages - 10, paginator.num_pages + 1)
            else:
                page_range = range(current_page - 5, current_page + 6)

        else:
            page_range = paginator.page_range

        # 默認是10個頁碼
        # if paginator.num_pages > 10:
        #     if current_page - 4 < 1:
        #         page_range = range(1, 11)
        #     elif current_page + 5 > paginator.num_pages:
        #         page_range = range(paginator.num_pages - 9, paginator.num_pages + 1)
        #     else:
        #         page_range = range(current_page - 5, current_page + 5)
        #
        # else:
        #     page_range = paginator.page_range
        return render(request, "book.html", {
            'paginator': paginator, "current_page": current_page, "page": page, "page_range": page_range
        })
views.py

(2)模板內容

<!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>
<h2>圖書列表</h2>
<ul>

{#    {% for book in page %}#}
{#        <li>{{ book.title }} --- {{ book.price }}</li>#}
{#    {% endfor %}#}
    {% for p in page %}
        <li>{{ p }}</li>
    {% endfor %}
</ul>
<nav aria-label="Page navigation">
    <ul class="pagination">
        {#        首頁#}
        <li>
            <a href="?page=1" aria-label="Previous">
                <span aria-hidden="true">首頁</span>
            </a>
        </li>
        {#  上一頁#}
        {% if page.has_previous %}
            <li>
                <a href="?page={{ page.previous_page_number }}" aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
        {% else %}
            <li class="disabled">
                <a href="javascript:void(0);" aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
            </li>
        {% endif %}

        {#        頁碼#}
{#        {% for num in paginator.page_range %}#}
        {% for num in page_range %}
            {% if num == current_page %}
                <li class="active"><a href="?page={{ num }}">{{ num }}</a></li>
            {% else %}
                <li><a href="?page={{ num }}">{{ num }}</a></li>
            {% endif %}

        {% endfor %}

        {#    下一頁#}
        {% if page.has_next %}
            <li>
                <a href="?page={{ page.next_page_number }}" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
        {% else %}
            <li class="disabled">
                <a href="javascript:void(0);" aria-label="Next">
                    <span aria-hidden="true">&raquo;</span>
                </a>
            </li>
        {% endif %}

        {#    尾頁#}
        <li>
            <a href="?page={{ paginator.num_pages }}" aria-label="Next">
                <span aria-hidden="true">尾頁</span>
            </a>
        </li>
    </ul>
</nav>
</body>
</html>
book.html

顯示的效果爲:html

 3.頁碼數設置奇數偶數優化版

(1)視圖函數

from django.shortcuts import render
from django.views import View  # 導入view
from app01 import models  # 導入模型
from django.core.paginator import Paginator  # 導入django分頁器
import math
# Create your views here.
# 頁碼數設置奇數偶數優化版:只要設置page_num 頁碼便可,奇數偶數都兼容
class Book(View):
    def get(self, request):
        # ############# django分頁器的基本使用進階版 ##################
        # 分頁器的基本語法
        book_list = models.Book.objects.all()

        # 示例分頁器對象
        paginator = Paginator(book_list, 2)      # 每頁顯示數據的數量
        current_page = int(
            request.GET.get("page") if request.GET.get("page") and request.GET.get("page").isdigit() else 1)
        # 獲取頁碼
        page = paginator.get_page(current_page)  # 獲取當前頁碼的全部數據
        page_num = 11    # 設置頁碼個數,設置須要的個數能夠爲奇數,偶數
        if paginator.num_pages > page_num:

            if current_page - math.floor(page_num/2) < 1:
                page_range = range(1, page_num+1)
            elif current_page + math.ceil((page_num-1)/2) > paginator.num_pages:
                page_range = range(paginator.num_pages - (page_num-1), paginator.num_pages + 1)
            else:
                page_range = range(current_page - math.ceil((page_num-1)/2), current_page + math.floor((page_num+1)/2))
        else:
            page_range = paginator.page_range

        return render(request, "book.html", {
            'paginator': paginator, "current_page": current_page, "page": page, "page_range": page_range
        })
views.py

(2)模板內容

模板內容同上2的模板內容java

效果圖:奇數git

效果圖:偶數數據庫

 二.自定製分頁器

前提是已經在一上面的基礎上,對視圖和模板進行修改npm

1. 自定製分頁器

(1)自定致分頁器類

class Paginator:
    def __init__(self, current_page, all_count, per_page=10, max_page_num=11):
        """
        封裝分頁相關數據
        :param current_page:  當前頁碼
        :param all_count:  數據庫中的數據總條數
        :param per_page:   每一個頁面顯示的數據條數
        :param max_page_num:  最多顯示的頁碼個數
        :param num_pages:  經過總條數/每一個頁面顯示的條數,求出總頁數
        """
        try:
            current_page = int(current_page)
        except Exception as e:
            current_page = 1
        if current_page < 1:
            current_page = 1
        self.current_page = current_page
        self.all_count = all_count
        self.per_page = per_page

        # 計算總頁數
        num_pages, temp = divmod(all_count, per_page)
        if temp:
            num_pages += 1
        self.num_pages = num_pages

        self.max_page_num = max_page_num  # 11
        self.page_count_half = int((self.max_page_num - 1) / 2)  # 5
        """
        self.num_pages=100
        per_page=8

        current_page =1     [0:8]
        current_page =2     [8:16]
        current_page =3     [16:24]
                            [(current_page-1)*per_page:current_page*per_page ]

        """

    @property
    def start(self):
        return (self.current_page - 1) * self.per_page

    @property
    def end(self):
        return self.current_page * self.per_page

    def page_html(self):
        # 若是總頁數小於self.max_page_num(最多顯示的頁碼個數)
        if self.num_pages <= self.max_page_num:
            page_start = 1
            page_end = self.num_pages + 1
        else:
            # 若是當前頁碼<=頁面上最多顯示11/2個頁碼時
            if self.current_page <= self.page_count_half:
                page_start = 1
                page_end = self.max_page_num + 1
            # 若是當前頁碼+最多顯示11/2 大於 總頁數時
            elif self.current_page + self.page_count_half > self.num_pages:
                page_start = self.num_pages - self.max_page_num + 1
                page_end = self.num_pages + 1
            else:
                page_start = self.current_page - self.page_count_half
                page_end = self.current_page + self.page_count_half + 1

        page_html_list = []

        # 首頁
        first_page = '<nav aria-label="Page navigation"><ul class="pagination"><li><a href="?page=1">首頁</a></li>'
        page_html_list.append(first_page)

        # 上一頁
        if self.current_page <= 1:
            prev_page = '<li class="disabled"><a href="javascript:void(0);">上一頁</a></li>'
        else:
            prev_page = '<li><a href="?page=%s">上一頁</a></li>' % (self.current_page - 1, )
        page_html_list.append(prev_page)

        # 顯示頁碼
        for i in range(page_start, page_end):
            if self.current_page == i:
                temp = '<li class="active"><a href="?page=%s">%s</a></li>' % (i, i)
            else:
                temp = '<li><a href="?page=%s">%s</a></li>' % (i, i)
            page_html_list.append(temp)

        # 下一頁
        if self.current_page >= self.num_pages:
            next_page = '<li class="disabled"><a href="javascript:void(0);">下一頁</a></li>'
        else:
            next_page = '<li><a href="?page=%s">下一頁</a></li>' % (self.current_page + 1)
        page_html_list.append(next_page)

        # 尾頁
        last_page = '<li><a href="?page=%s">尾頁</a></li></ul></nav>' % self.num_pages
        page_html_list.append(last_page)

        return "".join(page_html_list)
View Code

(2)視圖函數

from django.shortcuts import render, redirect
from django.views import View  # 導入view
from app01 import models  # 導入模型
from app01.page import Paginator # 導入自定製的分頁器

class Book(View):
    def get(self, request):
        book_list = models.Book.objects.all()
        current_page = request.GET.get('page')
    #                     當前頁數       數據總數            每頁數據量  分頁量
        paginator = Paginator(current_page, book_list.count(), 6, 11)
        book_list = book_list[paginator.start: paginator.end]

        return render(request, "book.html",
                      {"book_list": book_list, "paginator": paginator, "current_page": current_page})
views.py

(3)模板函數

<!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>
<h2>圖書列表</h2>
<ul>
    {% for book in book_list %}
        <li>{{ book.title }} --- {{ book.price }}</li>
    {% endfor %}
</ul>

{{ paginator.page_html|safe }}

</body>
</html>
book.html

效果圖:django

 

2.終極版本分頁器

爲何須要終極版本呢,由於上面都還有些缺陷,那就是若是請求頭還有其餘數據,換頁後會消失。bootstrap

(1)終極版自定義分頁類

class Paginator:
    def __init__(self, request, current_page, all_count, per_page=10, max_page_num=13):
        """
        封裝分頁相關數據
        :param current_page:  當前頁碼
        :param all_count:  數據庫中的數據總條數
        :param per_page:   每一個頁面顯示的數據條數
        :param max_page_num:  最多顯示的頁碼個數
        :param num_pages:  經過總條數/每一個頁面顯示的條數,求出總頁數
        """
        try:
            current_page = int(current_page)
        except Exception as e:
            current_page = 1
        if current_page < 1:
            current_page = 1
        self.current_page = current_page
        self.all_count = all_count
        self.per_page = per_page

        # 計算總頁數
        num_pages, temp = divmod(all_count, per_page)
        if temp:
            num_pages += 1
        self.num_pages = num_pages

        self.max_page_num = max_page_num  # 11
        self.page_count_half = int((self.max_page_num - 1) / 2)  # 5

        import copy
        self.url_args = copy.deepcopy(request.GET)
        print(self.url_args.urlencode())


        """
        self.num_pages=100
        per_page=8

        current_page =1     [0:8]
        current_page =2     [8:16]
        current_page =3     [16:24]
                            [(current_page-1)*per_page:current_page*per_page ]

        """

    @property
    def start(self):
        return (self.current_page - 1) * self.per_page

    @property
    def end(self):
        return self.current_page * self.per_page

    def page_html(self):
        # 若是總頁數小於self.max_page_num(最多顯示的頁碼個數)
        if self.num_pages <= self.max_page_num:
            page_start = 1
            page_end = self.num_pages + 1
        else:
            # 若是當前頁碼<=頁面上最多顯示11/2個頁碼時
            if self.current_page <= self.page_count_half:
                page_start = 1
                page_end = self.max_page_num + 1
            # 若是當前頁碼+最多顯示11/2 大於 總頁數時
            elif self.current_page + self.page_count_half > self.num_pages:
                page_start = self.num_pages - self.max_page_num + 1
                page_end = self.num_pages + 1
            else:
                page_start = self.current_page - self.page_count_half
                page_end = self.current_page + self.page_count_half + 1

        page_html_list = []

        # 首頁
        self.url_args['page'] = 1
        first_page = '<nav aria-label="Page navigation"><ul class="pagination"><li><a href="?%s">首頁</a></li>' % (self.url_args.urlencode())
        page_html_list.append(first_page)

        # 上一頁
        if self.current_page <= 1:
            prev_page = '<li class="disabled"><a href="javascript:void(0);">上一頁</a></li>'
        else:
            self.url_args['page'] = self.current_page - 1
            prev_page = '<li><a href="?%s">上一頁</a></li>' % (self.url_args.urlencode(), )
        page_html_list.append(prev_page)

        # 顯示頁碼
        for i in range(page_start, page_end):
            self.url_args['page'] = i
            if self.current_page == i:
                temp = '<li class="active"><a href="?%s">%s</a></li>' % (self.url_args.urlencode(), i)
            else:
                temp = '<li><a href="?%s">%s</a></li>' % (self.url_args.urlencode(), i)
            page_html_list.append(temp)

        # 下一頁
        if self.current_page >= self.num_pages:
            next_page = '<li class="disabled"><a href="javascript:void(0);">下一頁</a></li>'
        else:
            self.url_args['page'] = self.current_page + 1
            next_page = '<li><a href="?%s">下一頁</a></li>' % (self.url_args.urlencode(), )
        page_html_list.append(next_page)

        # 尾頁
        self.url_args['page'] = self.num_pages
        last_page = '<li><a href="?%s">尾頁</a></li></ul></nav>' % self.url_args.urlencode()
        page_html_list.append(last_page)

        return "".join(page_html_list)
page.py

(2)視圖函數

from django.shortcuts import render, redirect
from django.views import View  # 導入view
from app01 import models  # 導入模型
from app01.page import Paginator # 導入自定製的分頁器
class Book(View):
    def get(self, request):
        book_list = models.Book.objects.all()
        current_page = request.GET.get('page')
        paginator = Paginator(request, current_page, book_list.count(), 6, 11)
        book_list = book_list[paginator.start: paginator.end]

        return render(request, "book.html", {"book_list": book_list, "paginator": paginator, "current_page": current_page})
views.py

(3)模板函數

模板函數同上便可app

相關文章
相關標籤/搜索