分页器基本案例思路

Posted one-tom

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分页器基本案例思路相关的知识,希望对你有一定的参考价值。

app01/models.py

from django.db import models

# Create your models here.


class Book(models.Model):
    title=models.CharField(max_length=32)
    price=models.DecimalField(max_digits=6,decimal_places=2)

app01/page.py


import copy


class Pagination(object):
    def __init__(self, current_page, all_count, request, per_page=10, max_pager_num=11):
        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, tmp = divmod(all_count, per_page)
        if tmp:
            num_pages += 1

        self.num_pages = num_pages

        self.max_pager_num = max_pager_num
        self.pager_count_half = int((self.max_pager_num - 1) / 2)

        import copy
        self.parmas = copy.deepcopy(request.GET)

        print("++++++++", self.parmas.urlencode())

    @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):
        if self.num_pages <= self.max_pager_num:
            pager_start = 1
            pager_end = self.num_pages + 1

        else:
            if self.current_page <= self.pager_count_half:
                pager_start = 1
                pager_end = self.max_pager_num + 1

            elif (self.current_page + self.pager_count_half) > self.num_pages:
                pager_start = self.num_pages - self.max_pager_num + 1
                pager_end = self.num_pages + 1
            else:
                pager_start = self.current_page - self.pager_count_half
                pager_end = self.current_page + self.pager_count_half + 1

        pager_html_list = []

        # 首页,上一页
        self.parmas["page"]=1
        first_page = <nav aria-label="Page navigation"><ul class="pagination"><li><a href="?%s">首页</a></li> % (
            self.parmas.urlencode(),)
        pager_html_list.append(first_page)
        if self.current_page <= 1:
            prev_page = <li class="disabled"><a href="#">上一页</a></li>
        else:
            self.parmas["page"]=self.current_page-1
            prev_page = prev_page = <li><a href="?%s">上一页</a></li> % (self.parmas.urlencode(),)

        pager_html_list.append(prev_page)

        # 每一页页码

        for i in range(pager_start, pager_end):
            self.parmas["page"] = i
            if i == self.current_page:
                temp = <li class="active"><a href="?%s">%s</a></li> % (self.parmas.urlencode(), i,)
            else:
                temp = <li><a href="?%s">%s</a></li> % (self.parmas.urlencode(), i)

            pager_html_list.append(temp)

        # 最后一页,下一页
        self.parmas["page"] =self.current_page+1
        if self.current_page >= self.num_pages:
            next_page = <li class="disabled"><a href="#">下一页</a></li>
        else:
            next_page = <li><a href="?%s">下一页</a></li> % (self.parmas.urlencode(),)
        pager_html_list.append(next_page)

        self.parmas["page"]=self.num_pages
        last_page = <li><a href="?%s">尾页</a></li></ul></nav> % (self.parmas.urlencode())
        pager_html_list.append(last_page)

        return ‘‘.join(pager_html_list)

 

app01/views.py

def index(request):

    book_list1 = Book.objects.all()
    current_page = request.GET.get("page")
    print(">>",current_page)
    pagination = Pagination(current_page, book_list1.count(),request,per_page=5,max_pager_num=11)
    book_list = book_list1[pagination.start:pagination.end]
    return render(request, "index2.html", locals())

 

templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <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>
<div>
    <ul>

        {% for book in book_list %}

            <li>
                {{ book.title }}-----{{ book.price }}
            </li>
        {% endfor %}

    </ul>

    {{pagination.page_html|safe}}

</div>

</body>
</html>

 

项目总路由/urls.py

from django.contrib import admin
from django.urls import path

from app01 import views

urlpatterns = [
    path(admin/, admin.site.urls),
    path(index/, views.index),
]

 

以上是关于分页器基本案例思路的主要内容,如果未能解决你的问题,请参考以下文章

片段被视图分页器布局切割

FragmentStatePagerAdapter 视图分页器片段在活动重新创建后不显示

FragmentStatePagerAdapter视图分页器片段在重新创建活动后未显示

ZUI分页器的使用案例(ECLIPSE SMS项目)

SweetAler弹框插件与分页器插件

django-模型层之ajax续批量插入分页器