使用方法
"""
自定义分页组件的使用方法:
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. 剑指offer-数值的整数次方12

    class Solution: def Power(self, base, exponent): # write code here if base==0: return 0 if exponent= ...

  2. 九度OJ--Q1473

    import java.util.ArrayList;import java.util.Scanner; /* * 题目描述: * 大家都知道,数据在计算机里中存储是以二进制的形式存储的. * 有一天 ...

  3. 在 C/C++ 中使用 TensorFlow 预训练好的模型—— 间接调用 Python 实现

    现在的深度学习框架一般都是基于 Python 来实现,构建.训练.保存和调用模型都可以很容易地在 Python 下完成.但有时候,我们在实际应用这些模型的时候可能需要在其他编程语言下进行,本文将通过 ...

  4. 去西交大考PAT认证

    这周六去了西交大去考浙大PAT认证,为什么要写这个博客呢.因为...我不是西交大的学生,找考场就花了我很多时间,各种搜都找不到PAT的考场在哪. 在此记录一下,希望有有缘人再去西交大考试,可以少走点弯 ...

  5. php处理三级分类数据

    <?php // 链接数据库 $link = mysqli_connect('localhost','root','root'); if($link == null){ exit; } mysq ...

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

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

  7. Mininet实验 动态改变转发规则

    介绍 拓扑如下: 在该环境下,假设H1 ping H4,初始的路由规则是S1-S2-S5,一秒后,路由转发规则变为S1-S3-S5,再过一秒,规则变为S1-S4-S5,然后再回到最初的转发规则S1-S ...

  8. 软工实践 - 第二十五次作业 Beta 冲刺(3/7)

    队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/10116979.html 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过 ...

  9. beta版本冲刺七

    目录 组员情况 组员1:胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:恺琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:何宇恒 组员11:刘一好 展示组内最新 ...

  10. 并发(一) Semaphore

    Semaphore 控制对资源的并发访问数,构造时如果传参为1,则近似于ReentrantLock,差别在于锁的释放.可以一个线程获取锁,另外一个线程释放锁,在一些死锁处理的场合比较适用. 如上所示, ...