首页 > 其他分享 >30 Django分页组件

30 Django分页组件

时间:2022-10-10 17:00:34浏览次数:42  
标签:count self 30 queryset Django 组件 total pager page

pager.py:

"""
如果想要使用分页,需要以下两个步骤

def xxx():
    queryset = models.Customer.objects.filter(active=1).select_related('level', 'creator')  # select_related主动跨表

    pager = Pagination(request, queryset)

    context = {
        'queryset': queryset[pager.start: pager.end],
        'pager_string': pager.html(),
    }
    return render(request, 'customer_list.html', context)

页面上:
{% for row in queryset %}
    {{row.id}}
    ...
{% endfor %}

<ul class="pagination">
    {{ pager_string }}
</ul>

"""

from django.utils.safestring import mark_safe


class Pagination(object):
    """分页"""

    def __init__(self, request, queryset, per_page_count=10):
        total_count = queryset.count()  # 总数据条数
        # 计算出总共有多少页面
        self.total_count = total_count
        self.total_page, div = divmod(total_count, per_page_count)
        if div:
            self.total_page += 1

        page = request.GET.get('page')

        if not page:
            page = 1
        else:
            if not page.isdecimal():
                page = 1
            else:
                page = int(page)
                if page < 0:
                    page = 1
                else:
                    if page > self.total_page:
                        page = self.total_page

        self.page = page
        self.per_page_count = per_page_count

        self.start = (page - 1) * per_page_count
        self.end = page * per_page_count

    def html(self):
        pager_list = []
        # 极值问题:总共的页码数量
        if self.total_page <= 11:
            start_page = 1
            end_page = self.total_page
        else:
            # 总页码比较多,判断当前页 <=6 : 1~11
            if self.page <= 6:
                start_page = 1
                end_page = 11
            else:
                if (self.page + 5) > self.total_page:
                    start_page = self.total_page - 10
                    end_page = self.total_page
                else:
                    start_page = self.page - 5
                    end_page = self.page + 5

        for i in range(start_page, end_page + 1):
            if i == self.page:
                item = '<li class="active"><a href="?page={}">{}</a></li>'.format(i, i)
            else:
                item = '<li><a href="?page={}">{}</a></li>'.format(i, i)
            pager_list.append(item)
        pager_string = mark_safe(''.join(pager_list))

        return pager_string

标签:count,self,30,queryset,Django,组件,total,pager,page
From: https://www.cnblogs.com/mimiICC/p/16776334.html

相关文章

  • 父子组件的生命周期(执行顺序)
    结合父子组件之后,一个完整的父子组件生命周期:父beforeCreate->父created->父beforeMount->子beforeCreate->子created->子beforeMount->子mounted->父mounte......
  • Spring cloud alibaba--Feign微服务调用组件
    Springcloudalibaba--Feign微服务调用组件 https://blog.csdn.net/ZHANGLIZENG/article/details/119058973?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_rel......
  • php 获取今日、昨日、最近7天、最近30天等的时间戳方法
    //本周时间$now_start_time=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));$now_end_time=mktime(0,0,0,date('m'),date('d')-date('w')+1+7,date('Y'......
  • vuepress 搭建组件库文档
    项目地址项目地址:https://github.com/YolandaKisses/YolandaKisses.github.io演示地址:https://yolandakisses.github.io/目录结构├─docs│└─.vuepress│ ......
  • Nlog日志组件接入ES
    1、安装nuget包NLog.Targets.ElasticSearch2、调整配置文件Nlog.config<?xmlversion="1.0"encoding="utf-8"?><nlogxmlns="http://www.nlog-project.org/schemas/......
  • 37、linux下安装python3.6和django
    37.1、安装python:1、python介绍:python是一种面向对象的,解释型的计算机语言,它的特点是语法简介,优雅,简单易学。1989年诞生,Guido(龟叔)开发。编译型语言:代码在编译之后,编译成......
  • 30、css介绍
    30.1、css概述:css是CascadingStyleSheet的简称,中文称为层叠样式表,是用来控制网页数据表现的,可以使网页的表现与数据内容分离;30.2、css的四种引入方式:1、行内式:(1)在标记的st......
  • 49、django工程(cookie+session)
    49.1、介绍:1、cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生。cookie的工作原理是,由服务器产......
  • 46、django工程(view)
    46.1、djangoview视图函数说明:1、http请求中产生两个核心对象:(1)http请求:HttpRequest对象。(2)http响应:HttpResponse对象。2、views函数是接收用户请求,处理业务逻辑的函数:46.......
  • 摹客RP,新增图文选项卡组件!
    Hello,小伙伴们,又到了摹客的新功能播报时间。本月更新,摹客RP新增新的组件——​​图文选项卡组件​​,可用于快速制作手机项目底部导航等模块。摹客协作针对任务管理模块做了......