加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Django 分页组件

发布时间:2020-12-15 17:11:09 所属栏目:大数据 来源:网络整理
导读:前言 这个应该是上半年写的一个分页组件。没啥 BUG , Python 项目里都能用。 class Pagination(object): """通用分页组件""" def __init__(self,totalCount,currentPage,perPageNum=10,maxPageNum=7,): self.total_count = totalCount # 数据总个数 0 try:

前言

   这个应该是上半年写的一个分页组件。没啥BUGPython项目里都能用。

class Pagination(object):
    """通用分页组件"""

    def __init__(self,totalCount,currentPage,perPageNum=10,maxPageNum=7,):
        self.total_count = totalCount  # 数据总个数  0
        try:
            self.current_page = int(currentPage)  # 当前页  1
        except Exception as e:
            self.current_page = 1
        if self.current_page <= 0:
            self.current_page = 1
        self.per_page_item_num = perPageNum  # 每页显示的行数
        self.max_page_num = maxPageNum  # 按钮数量

    @property
    def start(self):
        return (self.current_page - 1) * self.per_page_item_num  # 0

    @property
    def end(self):
        return self.current_page * self.per_page_item_num  # 0

    @property
    def num_pages(self):
        '''总页数'''
        a,b = divmod(self.total_count,self.per_page_item_num)
        if not b:  # 如果存在多余的数据则多加一页页数
            return a
        return a + 1

    def pager_num_range(self):

        if self.num_pages < self.max_page_num:
            return range(1,self.num_pages + 1)  # range不取后面的值,所以+1
        '''下面是数据足够多的情况下'''
        part = int(self.max_page_num / 2)  # 拿到一半的数据。

        if self.current_page <= part:
            return range(1,self.max_page_num + 1)  # 动态效果

        if (self.current_page + part) >= self.num_pages:
            return range(self.num_pages - self.max_page_num + 1,self.num_pages + 1)

        return range(self.current_page - part,self.current_page + part + 1)

    def page_str(self):
        """该方法用于在插件中自动生成页码按钮"""
        page_list = []
        first_1 = '<ul class="pagination">'
        if self.total_count == 0:
            first = '<li class="disabled"><a>首页</a></li>'
        else:
            first = '<li><a href="?p=1">首页</a></li>'
        page_list.append(first_1)
        page_list.append(first)

        # ---------------上一页---------------------#
        if self.current_page == 1 or self.total_count == 0:
            prev = '<li class="disabled"><a>上一页</a></li>'
        else:
            # Pyhon3.8新增f语句格式化字符串
            # -1
            prev = f'<li><a href="?p={self.current_page - 1}">上一页</a></li>'
        page_list.append(prev)

        # ---------------数据与当前页样式---------------------#
        for i in self.pager_num_range():
            if i == self.current_page:
                temp = f'<li class="active" ><a href="?p={i}">{i}</a></li>'
            else:
                temp = f'<li ><a href="?p={i}">{i}</a></li>'
            page_list.append(temp)
        # ---------------下一页---------------------#
        if self.current_page == self.num_pages or self.total_count == 0:
            nex_t = '<li class="disabled" ><a>下一页</a></li>'
        else:
            nex_t = f'<li><a href="?p={self.current_page + 1}">下一页</a></li>'
        page_list.append(nex_t)
        if self.total_count == 0:
            last = f'<li  class="disabled" ><a>尾页</a></li>'
        else:
            last = f'<li><a href="?p={self.num_pages}">尾页</a></li>'
        page_list.append(last)
        last_1 = "</ul>"
        page_list.append(last_1)

        return ' '.join(page_list)

使用方式

   使用很简单,这里就依Django为例。

from django.shortcuts import render
from app01 import models
from utils.page import *

# Create your views here.

def test(request):

    currentPage = request.GET.get("p")
    res_queryset = models.UserTest.objects.all() 

    page_obj = Pagination(
        totalCount=res_queryset.count(),# 传入数据总数量
        currentPage=currentPage,# 当前页
    )
  
    page_queryset = res_queryset[page_obj.start:page_obj.end]  # 真正显示的数据
    return render(request,"test.html",locals())

   同时,你还需要在前端进行调用。

<body>

	<!-- 展示的数据 -->
	{% for user_obj in page_queryset %}
		<p>{{user_obj.name}}</p>
	{% endfor %} 
	<!-- 调用自定义分页 -->
	{{page_obj.page_str|safe}}

</body>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读