Django 分頁組件替換自定義分頁

Django的分頁器(paginator)

總之不太好用咱們仍是用本身的好一些 css

自定義分頁器

分頁實現源碼

"""
自定義分頁組件
"""


class Pagination(object):
    def __init__(self, current_page, all_count, base_url, params, per_page_num=8, pager_count=11, ):
        """
        封裝分頁相關數據
        :param current_page: 當前頁
        :param all_count:    數據庫中的數據總條數
        :param base_url: 分頁中顯示的URL前綴
        :param params:  request.get 對象
        :param per_page_num: 每頁顯示的數據條數
        :param pager_count:  最多顯示的頁碼個數
        """
        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_num = per_page_num
        self.base_url = base_url
        all_pager, tmp = divmod(all_count, per_page_num)
        if tmp:
            all_pager += 1
        self.all_pager = all_pager
        self.pager_count = pager_count
        self.pager_count_half = int((pager_count - 1) / 2)

        import copy
        params = copy.deepcopy(params)
        params._mutable = True
        self.params = params  # self.params : {"page":77,"title":"python","nid":1}

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

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

    def page_html(self):
        # 若是總頁碼 < 11個:
        if self.all_pager <= self.pager_count:
            pager_start = 1
            pager_end = self.all_pager + 1
        # 總頁碼  > 11
        else:
            # 當前頁若是<=頁面上最多顯示(11-1)/2個頁碼
            if self.current_page <= self.pager_count_half:
                pager_start = 1
                pager_end = self.pager_count + 1
            # 當前頁大於5
            else:
                # 頁碼翻到最後
                if (self.current_page + self.pager_count_half) > self.all_pager:
                    pager_start = self.all_pager - self.pager_count + 1
                    pager_end = self.all_pager + 1
                else:
                    pager_start = self.current_page - self.pager_count_half
                    pager_end = self.current_page + self.pager_count_half + 1
        page_html_list = []
        self.params["page"] = 1
        first_page = '<li><a href="%s?%s">首頁</a></li>' % (self.base_url, self.params.urlencode(),)
        page_html_list.append(first_page)
        if self.current_page <= 1:
            prev_page = '<li class="disabled"><a href="#">上一頁</a></li>'
        else:
            self.params["page"] = self.current_page - 1
            prev_page = '<li><a href="%s?%s">上一頁</a></li>' % (self.base_url, self.params.urlencode(),)
        page_html_list.append(prev_page)
        for i in range(pager_start, pager_end):
            self.params["page"] = i
            if i == self.current_page:
                temp = '<li class="active"><a href="%s?%s">%s</a></li>' % (self.base_url, self.params.urlencode(), i,)
            else:
                temp = '<li><a href="%s?%s">%s</a></li>' % (self.base_url, self.params.urlencode(), i,)
            page_html_list.append(temp)
        if self.current_page >= self.all_pager:
            next_page = '<li class="disabled"><a href="#">下一頁</a></li>'
        else:
            self.params["page"] = self.current_page + 1
            next_page = '<li><a href="%s?%s">下一頁</a></li>' % (self.base_url, self.params.urlencode(),)
        page_html_list.append(next_page)
        self.params["page"] = self.all_pager
        last_page = '<li><a href="%s?%s">尾頁</a></li>' % (self.base_url, self.params.urlencode(),)
        page_html_list.append(last_page)
        return ''.join(page_html_list)
page.py

視圖函數調用

from django.shortcuts import render,HttpResponse

# Create your views here.


from .models import *


def index(request):

    '''
    book_list=[]
    for  i in range(500):
        book_obj=Book(title="book_%s"%i,price=i*i)
        book_list.append(book_obj)
    Book.objects.bulk_create(book_list)
    '''
    current_page = request.GET.get("page", 1)
    all_count = Book.objects.all().count()
    base_url = request.path  # /index/
    from app01.utils.page import Pagination
    pagination = Pagination(int(current_page),all_count,base_url,request.GET, per_page_num=8, pager_count=11,)

    # def __init__(self, current_page, all_count, base_url, params, per_page_num=8, pager_count=11, ):
    book_list = Book.objects.all()[pagination.start:pagination.end]

    from django.http.request import QueryDict
    # dic = QueryDict(mutable=True)
    # dic["info"] = 123
    # print(type(request.GET))
    # request.GET["info"]=123
    import copy
    params = copy.deepcopy(request.GET)
    params["xxx"] = 123
    return render(request, "index.html", locals())
views.py

模板使用展現

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
          integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>


<ul>
    {% for foo in book_list %}
        <li>{{ foo }}</li>
    {% endfor %}

</ul>
<nav>
    <ul class="pagination">
        {{ pagination.page_html|safe }}
    </ul>
</nav>


</body>
</html>
index.html
相關文章
相關標籤/搜索