欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

自定义分页模板(银角大王版)

程序员文章站 2022-06-21 22:18:08
当数据库中数据有很多,我们通常会在前端页面做分页展示。 分页的数据可以在前端页面实现,也可以在后端实现分页。 后端实现分页的原理就是每次只请求一页数据。 准备工作 我们使用脚本批量创建一些测试数据(将下面的代码保存到bulk_create.py文件中放到Django项目的根目录,直接执行即可。) i ......

当数据库中数据有很多,我们通常会在前端页面做分页展示。

分页的数据可以在前端页面实现,也可以在后端实现分页。

后端实现分页的原理就是每次只请求一页数据。

准备工作

我们使用脚本批量创建一些测试数据(将下面的代码保存到bulk_create.py文件中放到django项目的根目录,直接执行即可。)

import os

if __name__ == "__main__":
    os.environ.setdefault("django_settings_module", "about_orm.settings")

    import django
    django.setup()

    from app01 import models
    bulk_obj = (models.publisher(name='沙河第{}出版社'.format(i)) for i in range(300))
    models.publisher.objects.bulk_create(bulk_obj)

自定义分页

def publisher_list(request):
    # 从url中取当前访问的页码数
    try:
        current_page = int(request.get.get('page'))
    except exception as e:
        # 取不到或者页码数不是数字都默认展示第1页
        current_page = 1
    # 总数据量
    total_count = models.publisher.objects.count()
    # 定义每页显示多少条数据
    per_page = 10
    # 计算出总页码数
    total_page, more = divmod(total_count, per_page)
    if more:
        total_page += 1
    # 定义页面上最多显示多少页码(为了左右对称,一般设为奇数)
    max_show = 11
    half_show = max_show // 2
    # 计算一下页面显示的页码范围
    if total_page <= max_show:  # 总页码数小于最大显示页码数
        page_start = 1
        page_end = total_page
    elif current_page + half_show >= total_page:  # 右边越界
        page_end = total_page
        page_start = total_page - max_show
    elif current_page - half_show <= 1:  # 左边越界
        page_start = 1
        page_end = max_show
    else:  # 正常页码区间
        page_start = current_page - half_show
        page_end = current_page + half_show
    # 数据索引起始位置
    data_start = (current_page-1) * per_page
    data_end = current_page * per_page

    publisher_list = models.publisher.objects.all()[data_start:data_end]

    # 生成页面上显示的页码
    page_html_list = []
    page_html_list.append('<nav aria-label="page navigation"><ul class="pagination">')
    # 加首页
    first_li = '<li><a href="/publisher_list/?page=1">首页</a></li>'
    page_html_list.append(first_li)
    # 加上一页
    if current_page == 1:
        prev_li = '<li><a href="#"><span aria-hidden="true">&laquo;</span></a></li>'
    else:
        prev_li = '<li><a href="/publisher_list/?page={}"><span aria-hidden="true">&laquo;</span></a></li>'.format(current_page - 1)
    page_html_list.append(prev_li)
    for i in range(page_start, page_end + 1):
        if i == current_page:
            li_tag = '<li class="active"><a href="/publisher_list/?page={0}">{0}</a></li>'.format(i)
        else:
            li_tag = '<li><a href="/publisher_list/?page={0}">{0}</a></li>'.format(i)
        page_html_list.append(li_tag)
    # 加下一页
    if current_page == total_page:
        next_li = '<li><a href="#"><span aria-hidden="true">&raquo;</span></a></li>'
    else:
        next_li = '<li><a href="/publisher_list/?page={}"><span aria-hidden="true">&raquo;</span></a></li>'.format(current_page + 1)
    page_html_list.append(next_li)
    # 加尾页
    page_end_li = '<li><a href="/publisher_list/?page={}">尾页</a></li>'.format(total_page)
    page_html_list.append(page_end_li)
    page_html_list.append('</ul></nav>')
    page_html = "".join(page_html_list)
    return render(request, "publisher_list.html", {"publisher_list": publisher_list, "page_html": page_html})
class pagination(object):
    """自定义分页(bootstrap版)"""
    def __init__(self, current_page, total_count, base_url, per_page=10, max_show=11):
        """
        :param current_page: 当前请求的页码
        :param total_count: 总数据量
        :param base_url: 请求的url
        :param per_page: 每页显示的数据量,默认值为10
        :param max_show: 页面上最多显示多少个页码,默认值为11
        """
        try:
            self.current_page = int(current_page)
        except exception as e:
            # 取不到或者页码数不是数字都默认展示第1页
            self.current_page = 1
        # 定义每页显示多少条数据
        self.per_page = per_page
        # 计算出总页码数
        total_page, more = divmod(total_count, per_page)
        if more:
            total_page += 1
        self.total_page = total_page
        # 定义页面上最多显示多少页码(为了左右对称,一般设为奇数)
        self.max_show = max_show
        self.half_show = max_show // 2
        self.base_url = base_url

    @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.total_page <= self.max_show:  # 总页码数小于最大显示页码数
            page_start = 1
            page_end = self.total_page
        elif self.current_page + self.half_show >= self.total_page:  # 右边越界
            page_end = self.total_page
            page_start = self.total_page - self.max_show
        elif self.current_page - self.half_show <= 1:  # 左边越界
            page_start = 1
            page_end = self.max_show
        else:  # 正常页码区间
            page_start = self.current_page - self.half_show
            page_end = self.current_page + self.half_show
        # 生成页面上显示的页码
        page_html_list = []
        page_html_list.append('<nav aria-label="page navigation"><ul class="pagination">')
        # 加首页
        first_li = '<li><a href="{}?page=1">首页</a></li>'.format(self.base_url)
        page_html_list.append(first_li)
        # 加上一页
        if self.current_page == 1:
            prev_li = '<li><a href="#"><span aria-hidden="true">&laquo;</span></a></li>'
        else:
            prev_li = '<li><a href="{}?page={}"><span aria-hidden="true">&laquo;</span></a></li>'.format(
                self.base_url, self.current_page - 1)
        page_html_list.append(prev_li)
        for i in range(page_start, page_end + 1):
            if i == self.current_page:
                li_tag = '<li class="active"><a href="{0}?page={1}">{1}</a></li>'.format(self.base_url, i)
            else:
                li_tag = '<li><a href="{0}?page={1}">{1}</a></li>'.format(self.base_url, i)
            page_html_list.append(li_tag)
        # 加下一页
        if self.current_page == self.total_page:
            next_li = '<li><a href="#"><span aria-hidden="true">&raquo;</span></a></li>'
        else:
            next_li = '<li><a href="{}?page={}"><span aria-hidden="true">&raquo;</span></a></li>'.format(
                self.base_url, self.current_page + 1)
        page_html_list.append(next_li)
        # 加尾页
        page_end_li = '<li><a href="{}?page={}">尾页</a></li>'.format(self.base_url, self.total_page)
        page_html_list.append(page_end_li)
        page_html_list.append('</ul></nav>')
        return "".join(page_html_list)

 

后端

def publisher_list(request):
    # 从url中取当前访问的页码数
    current_page = int(request.get.get('page'))
    # 比len(models.publisher.objects.all())更高效
    total_count = models.publisher.objects.count()
    page_obj = pagination(current_page, total_count, request.path_info)
    data = models.publisher.objects.all()[page_obj.start:page_obj.end]
    page_html = page_obj.page_html()
    return render(request, "publisher_list.html", locals())


前端

循环显示那:
{% for i in publisher_list %}
....
{% endfor %}

{{ page_obj.page_html|safe }}

django内置分页

from django.shortcuts import render
from django.core.paginator import paginator, emptypage, pagenotaninteger

l = []
for i in range(999):
    l.append(i)

def index(request):
    current_page = request.get.get('p')

    paginator = paginator(l, 10)
    # per_page: 每页显示条目数量
    # count:    数据总个数
    # num_pages:总页数
    # page_range:总页数的索引范围,如: (1,10),(1,200)
    # page:     page对象
    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, 'index.html', {'posts': posts})
<!doctype html>
<html>
<head lang="en">
    <meta charset="utf-8">
    <title></title>
</head>
<body>
<ul>
    {% for item in posts %}
        <li>{{ item }}</li>
    {% endfor %}
</ul>

<div class="pagination">
      <span class="step-links">
        {% if posts.has_previous %}
            <a href="?p={{ posts.previous_page_number }}">previous</a>
        {% endif %}
          <span class="current">
            page {{ posts.number }} of {{ posts.paginator.num_pages }}.
          </span>
          {% if posts.has_next %}
              <a href="?p={{ posts.next_page_number }}">next</a>
          {% endif %}
      </span>

</div>
</body>
</html>