使用方法
"""
自定义分页组件的使用方法:
pager_obj = Pagination(request.GET.get('page',1),len(HOST_LIST),request.path_info,request.GET)
host_list = HOST_LIST[pager_obj.start:pager_obj.end]#每页显示的长度
html = pager_obj.page_html()#返回的页码
return render(request,'hosts.html',{'host_list':host_list,"page_html":html})
"""
class Pagination(object):
def __init__(self, current_page, totalCount, base_url,parmas, per_page_count=10, max_page_count=11):
try:
current_page = int(current_page)
except:
current_page = 1
if current_page < 1:
current_page = 1
# 当前页
self.current_page = current_page
# 总记录数/总条数
self.totalCount = totalCount
# 每页显示的数量
self.per_page_count = per_page_count max_page_num, v = divmod(totalCount, per_page_count)
if v:
max_page_num += 1 # 总页数
self.max_page_num = max_page_num
# 显示页码的个数
# 页面上默认显示11个页面(当前页在中间)
self.max_page_count = max_page_count self.half_page_count = int((self.max_page_count - 1) / 2)
# URL前缀
self.base_url = base_url import copy
parmas=copy.deepcopy(parmas)
print(parmas,'------******')
self.parmas=parmas
# 每页开始的记录
@property
def start(self):
return (self.current_page - 1) * self.per_page_count # 每页结束的记录
@property
def end(self):
return self.per_page_count * self.current_page # 页码显示
def page_html(self): if self.max_page_num < self.max_page_count:
page_start = 1
page_end = self.max_page_num
else:
if self.current_page < self.half_page_count:
page_start = 1
page_end = self.max_page_count else:
if (self.current_page + self.half_page_count) > self.max_page_num:
page_start = self.max_page_num - self.max_page_count + 1
page_end = self.max_page_num else:
page_start = self.current_page - self.half_page_count
page_end = self.current_page + self.half_page_count html_list = []
# 首页
self.parmas['page'] = 1
first_page = '<li><a href="%s?%s">首页</a></li>' % (self.base_url,self.parmas.urlencode())
html_list.append(first_page) # 上一页 if self.current_page == 1:
pre_page='<li class="disabled"><a href="#" aria-label="Previous" ><span aria-hidden="true">&laquo;</span></a></li>'
#pre_page = "<a href='%s?%s' >上一页</a>" % (self.base_url, self.parmas.urlencode())
else:
self.parmas['page'] =self.current_page-1
#pre_page = "<a href='%s?%s'>上一页</a>" % (self.base_url, self.parmas.urlencode())
pre_page = '<li><a href="%s?%s" aria-label="Previous"><span aria-hidden="true"> &laquo; </span></a> </li>'%(
self.base_url, self.parmas.urlencode())
html_list.append(pre_page) for i in range(page_start, page_end + 1):
self.parmas['page'] =i
if i == self.current_page:
#tmp = "<a href='%s?%s' class='active'>%s</a>" % (self.base_url, self.parmas.urlencode(), i)
tmp='<li class="active"><a href="%s?%s" >%s</a></li>'%(self.base_url, self.parmas.urlencode(), i)
else:
tmp='<li><a href="%s?%s" >%s</a></li>'%(self.base_url, self.parmas.urlencode(), i) html_list.append(tmp) # 下一页
if self.current_page == self.max_page_num: #next_page = "<a href='%s?%s'>下一页</a>" % (self.base_url, self.parmas.urlencode())
next_page='<li class="disabled"><a aria-label="Next" ><span aria-hidden="true">&raquo;</span></a></li>'
else:
self.parmas['page']=self.current_page+1
#next_page = "<a href='%s?%s'>下一页</a>" % (self.base_url,self.parmas.urlencode())
next_page= next_page='<li><a href="%s?%s" aria-label="Next" disabled="disabled"><span aria-hidden="true">&raquo;</span></a></li>'% (self.base_url,self.parmas.urlencode()) html_list.append(next_page) self.parmas['page']=self.max_page_num
last_url = "<li><a href='%s?%s'>尾页</a></li>" % (self.base_url, self.parmas.urlencode())
html_list.append(last_url)
return ''.join(html_list)

pager

python-自定义分页组件的更多相关文章

  1. Python自定义分页组件

    为了防止XSS即跨站脚本攻击,需要加上 safe # 路由 from django.conf.urls import url from django.contrib import admin from ...

  2. 基于 Python 的自定义分页组件

    基于 Python 的自定义分页组件 分页是网页中经常用到的地方,所以将分页功能分出来,作为一个组件可以方便地使用. 分页实际上就是不同的 url ,通过这些 url 获取不同的数据. 业务逻辑简介 ...

  3. Django框架---- 自定义分页组件

    分页的实现与使用 class Pagination(object): """ 自定义分页 """ def __init__(self,cur ...

  4. Angular4.+ ngx-bootstrap Pagination 自定义分页组件

    Angular4 随笔(二)  ——自定义分页组件 1.简介 本组件主要是实现了分页组件显示功能,通过使用 ngx-bootstrap Pagination分页组件实现. 基本逻辑: 1.创建一个分页 ...

  5. vue 自定义分页组件

    vue2.5自定义分页组件,可设置每页显示条数,带跳转框直接跳转到相应页面 Pagination.vue 效果如下图: all: small(只显示数字和上一页和下一页): html <temp ...

  6. python web 分页组件

    闲来无事便写了一个易使用,易移植的Python Web分页组件.使用的技术栈是Python.Django.Bootstrap. 既然是易使用.易移植的组件,首先介绍一下其在django框架中的调用方式 ...

  7. vue自定义分页组件---切图网

    vue2.5自定义分页组件 Pagination.vue,可设置每页显示条数,带跳转框直接跳转到相应页面,亲测有用.目前很多框架自带有分页组件比如elementUI,不过在面对一个拿到PSD稿,然后重 ...

  8. angular自定义分页组件(实用)

    功能描述:分页,点击按钮或者下一页获取分页接口,同时active到对应页码. html模块: <page page-count="totalPage" on-click-pa ...

  9. Flex4 自定义分页组件

    自己写的Flex4分页组件,去伪存真,只实现基本的分页功能,数据过滤神马的都不应该是分页组件干的活,有呆毛才有真相: [源代码下载] Flex自从转手给Apache后人气急跌,本人也很捉鸡,尽管Apa ...

  10. jquery ajax自定义分页组件(jquery.loehpagerv1.0)原创

    简单的两个步骤截可调用 <script src="<%=basePath%>/resources/js/jquery-1.7.1.min.js"></ ...

随机推荐

  1. json 处理日期格式

    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8") private Date createT ...

  2. Linux-Shell脚本编程-学习-1-Linux基本命令

    在学习Linux-Shell脚本编程之前,我们需要学习一定的Linux基本命令,不然在后面学习Shell脚本编程的的时候,我们就呵呵了. 我学习所用的系统是Ubuntu 16.04版本 也没有什么规则 ...

  3. Linux常用命令及搭建测试环境

    题外话:三大操作系统------Linux.Unix.Windows,Unix系统如常见的Mac OS,Linux的很多命令跟Unix是通用的,所以就有一些开发人猿喜欢用苹果的原因.Linux发行版特 ...

  4. python接口测试(三)——Excell文件读取进行参数化

    python进行http请求时,需要对参数进行参数化,此时就可以运用Excel进行,具体如下: 1.梳理出请求中那些参数需要参数化,然后新建一个Excel,如图: 2.读取Excel中的内容,在读取前 ...

  5. 使用hibernate连接Oracle时的权限问题

    在使用hibernate对象关系映射连接和创建表的时候,会涉及到很多权限问题,有些数据库管理会将权限设的很细,我们可以根据后台日志错误和异常信息作出判断. 比如下图所示这个错误(这是我在给银行投产系统 ...

  6. BZOJ 3998 TJOI2015 弦论 后缀自动机+DAG上的dp

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3998 题意概述:对于一个给定长度为N的字符串,求它的第K小子串是什么,T为0则表示不同位置 ...

  7. HDU 4717 The Moving Points(三分法)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description There are N points in total. Every point moves in certain direction and certain speed. W ...

  8. POJ 2166 Heapsort(递推)

    Description A well known algorithm called heapsort is a deterministic sorting algorithm taking O(n l ...

  9. MySQL 5.6查看数据库的大小

    1. use information_schema; 2. select concat(round(sum(data_length/1024/1024),2),'MB') as data from t ...

  10. MySQL中Alter用法小结

    alter 方法是我们在处理MySQL数据库中一个常见的方法,能帮助我们更好的处理数据库中的表 1.增加 数据库中表的字段:alter table table_name add [column] co ...