Python分页组件
分页组件的实现:
class Pagination(object):
"""
自定义分页
"""
def __init__(self,current_page,total_count,base_url,params,per_page_count=10,max_pager_count=11):
try:
current_page = int(current_page)
except Exception as e:
current_page = 1
if current_page <=0:
current_page = 1
self.current_page = current_page
# 数据总条数
self.total_count = total_count # 每页显示10条数据
self.per_page_count = per_page_count # 页面上应该显示的最大页码
max_page_num, div = divmod(total_count, per_page_count)
if div:
max_page_num += 1
self.max_page_num = max_page_num # 页面上默认显示11个页码(当前页在中间)
self.max_pager_count = max_pager_count
self.half_max_pager_count = int((max_pager_count - 1) / 2) # URL前缀
self.base_url = base_url # request.GET
import copy
params = copy.deepcopy(params)
params._mutable = True
# 包含当前列表页面所有的搜索条件
# {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]}
# self.params[page] = 8
# self.params.urlencode()
# source=2&status=2&gender=2&consultant=1&page=8
# href="/hosts/?source=2&status=2&gender=2&consultant=1&page=8"
# href="%s?%s" %(self.base_url,self.params.urlencode())
self.params = params @property 这个property 的作用是,在调用的时候不用加上括号,直接使用self.start即可
def start(self):
return (self.current_page - 1) * self.per_page_count @property
def end(self):
return self.current_page * self.per_page_count def page_html(self):
# 如果总页数 <= 11
if self.max_page_num <= self.max_pager_count:
pager_start = 1
pager_end = self.max_page_num
# 如果总页数 > 11
else:
# 如果当前页 <= 5
if self.current_page <= self.half_max_pager_count:
pager_start = 1
pager_end = self.max_pager_count
else:
# 当前页 + 5 > 总页码
if (self.current_page + self.half_max_pager_count) > self.max_page_num:
pager_end = self.max_page_num
pager_start = self.max_page_num - self.max_pager_count + 1 #倒这数11个
else:
pager_start = self.current_page - self.half_max_pager_count
pager_end = self.current_page + self.half_max_pager_count page_html_list = []
# {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]}
# 首页
self.params['page'] = 1
first_page = '<li><a href="%s?%s">首页</a></li>' % (self.base_url,self.params.urlencode(),)
page_html_list.append(first_page) # 上一页
self.params["page"] = self.current_page - 1
if self.params["page"] < 1:
pervious_page = '<li class="disabled"><a href="%s?%s" aria-label="Previous">上一页</span></a></li>' % (
self.base_url, self.params.urlencode())
else:
pervious_page = '<li><a href = "%s?%s" aria-label = "Previous" >上一页</span></a></li>' % (
self.base_url, self.params.urlencode())
page_html_list.append(pervious_page) # 中间页码
for i in range(pager_start, pager_end + 1):
self.params['page'] = i
if i == self.current_page:
temp = '<li class="active"><a href="%s?%s">%s</a></li>' % (self.base_url,self.params.urlencode(), i,)
else:
temp = '<li><a href="%s?%s">%s</a></li>' % (self.base_url,self.params.urlencode(), i,)
page_html_list.append(temp) # 下一页
self.params["page"] = self.current_page + 1
if self.params["page"] > self.max_page_num:
self.params["page"] = self.current_page
next_page = '<li class="disabled"><a href = "%s?%s" aria-label = "Next">下一页</span></a></li >' % (
self.base_url, self.params.urlencode())
else:
next_page = '<li><a href = "%s?%s" aria-label = "Next">下一页</span></a></li>' % (
self.base_url, self.params.urlencode())
page_html_list.append(next_page) # 尾页
self.params['page'] = self.max_page_num
last_page = '<li><a href="%s?%s">尾页</a></li>' % (self.base_url, self.params.urlencode(),)
page_html_list.append(last_page) return ''.join(page_html_list)
分页组件的使用
"""
自定义分页组件的使用方法:
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() ----->这里返回的是page_html_list
return render(request,'hosts.html',{'host_list':host_list,"page_html":html})
"""
按照上面我们在前端之间传入数据到py文件中
pager_obj = Pagination(request.GET.get('page',1),len(HOST_LIST),request.path_info,request.GET)
request.GET.get('page',1) ------》 current_page
len(HOST_LIST) ------》 total_count(这里要是数据库的话,我们可以取总条数)
request.path_info ------》 base_Url
request.GET ------》 params
原文章转自:
http://www.cnblogs.com/haiyan123/p/8065353.html
Python分页组件的更多相关文章
- Python自定义分页组件
为了防止XSS即跨站脚本攻击,需要加上 safe # 路由 from django.conf.urls import url from django.contrib import admin from ...
- 基于 Python 的自定义分页组件
基于 Python 的自定义分页组件 分页是网页中经常用到的地方,所以将分页功能分出来,作为一个组件可以方便地使用. 分页实际上就是不同的 url ,通过这些 url 获取不同的数据. 业务逻辑简介 ...
- python web 分页组件
闲来无事便写了一个易使用,易移植的Python Web分页组件.使用的技术栈是Python.Django.Bootstrap. 既然是易使用.易移植的组件,首先介绍一下其在django框架中的调用方式 ...
- Django 分页组件替换自定义分页
Django的分页器(paginator) 总之不太好用我们还是用自己的好一些 自定义分页器 分页实现源码 """ 自定义分页组件 """ ...
- 前后端分离djangorestframework——分页组件
Pagination 为什么要分页也不用多说了,大家都懂,DRF也自带了分页组件 这次用 前后端分离djangorestframework——序列化与反序列化数据 文章里用到的数据,数据库用的my ...
- 比Django官方实现更好的分页组件+Bootstrap整合
前言 Django全家桶自带的分页组件只能说能满足分页这个功能,但是没那么好用就是了 Django的分页效果 django-pure-pagination分页效果 使用方法 首先安装: pip ins ...
- 基于Vue.js的表格分页组件
有一段时间没更新文章了,主要是因为自己一直在忙着学习新的东西而忘记分享了,实在惭愧. 这不,大半夜发文更一篇文章,分享一个自己编写的一个Vue的小组件,名叫BootPage. 不了解Vue.js的童鞋 ...
- angular-ui分页组件
http://angular-ui.github.io/bootstrap/#/pagination 分页组件只提供生成分页按钮,数据的显示需要使用ng-repeat, 注意设置 items-per- ...
- asp.net mvc4+mysql做一个简单分页组件(部分视图)
在开始做mysql分页功能组件前,便设定的是要有一定可复用性.先在项目里Views文件夹下右键新建名为_PaginationComponent.cshtml,这里html及css我采用的bootstr ...
随机推荐
- JS变量声明方式
在JavaScript中有三种声明变量的方式:const var let const:用于声明常量.注意:定义的变量的时候,必须同时初始化,且其值之后不可以修改. var:最常用的声明变量关键字. ...
- 机器学习模型从windows下 spring上传到预发布会导致模型不可加载
1.通过上传到redis,程序通过redis拉取模型,解决问题. 2.问题原因初步思考为windows下模型文件上传到 linux导致,待继续跟进查找.
- phpmyadmin#1045 无法登录 MySQL 服务器
使用lnmp安装wordpress在登录phpmyadmin数据库的时候诡异的出现了 phpmyadmin#1045 无法登录 MySQL 服务器,解决方法如下: 1 修改root的密码—不可以行 ...
- 关于ajax请求后js绑定事件失效问题解决方法
<script> $(function(){ $(document).on('click', '.add' ,function(){ window.location.href=" ...
- 2000万行表从SqlServer转移到Mongodb
就是记录一下操作过程,备忘,没什么难的
- 线段树入门&lazy思想
线段树将区间分成若干个子区间,子区间又继续分,直到区间为一个点(区间左值等于右值) 对于父区间[a,b],其子区间为[a,(a+b)/2]和[(a+b)/2+1,b] 用于求区间的值,如区间最值.区间 ...
- Codeforces Round #428 (Div. 2) D. Winter is here 容斥
D. Winter is here 题目连接: http://codeforces.com/contest/839/problem/D Description Winter is here at th ...
- vim设置一个tab为4个空格,设置行号
这我就再当前用户下,不是root权限.本身是没有.vimrc这个文件的 vim ~/.vimrc 新建了这个文件 然后在其中输入 保存. 这样设置完就一个tab是4个空格,并且就有行号了.重新打开v ...
- python正则表达式里引入变量
import re def reg_exp(senten): jiqiren = "阿童木" matchObj1 = re.search( r'(你(.*?)(男|女))|(机器( ...
- RS485 VS 20mA 电流环
RS485采用差分信号负逻辑,+2V-+6V表示“0”,- 6V-- 2V表示“1”.RS485有两线制和四线制两种接线,四线制只能实现点对点的通信方式,现很少采用,现在多采用的是两线制接线方式,这种 ...