from django.utils.safestring import mark_safe

 class Page:
def __init__(self, current_page, data_count, per_page_count=2, pager_num=7):
self.current_page = current_page
self.data_count = data_count
self.per_page_count = per_page_count
self.pager_num = pager_num @property
def start(self):
return (self.current_page - 1) * self.per_page_count @property
def end(self):
return self.current_page * self.per_page_count @property
def total_count(self):
v, y = divmod(self.data_count, self.per_page_count)
if y:
v += 1
return v def page_str(self, base_url):
page_list = [] if self.total_count < self.pager_num:
start_index = 1
end_index = self.total_count + 1
else:
if self.current_page <= (self.pager_num + 1) / 2:
start_index = 1
end_index = self.pager_num + 1
else:
start_index = self.current_page - (self.pager_num - 1) / 2
end_index = self.current_page + (self.pager_num + 1) / 2
if (self.current_page + (self.pager_num - 1) / 2) > self.total_count:
end_index = self.total_count + 1
start_index = self.total_count - self.pager_num + 1 if self.current_page == 1:
prev = '<a class="page" href="javascript:void(0);">上一页</a>'
else:
prev = '<a class="page" href="%s?p=%s">上一页</a>' % (base_url, self.current_page - 1,)
page_list.append(prev) for i in range(int(start_index), int(end_index)):
if i == self.current_page:
temp = '<a class="page active" href="%s?p=%s">%s</a>' % (base_url, i, i)
else:
temp = '<a class="page" href="%s?p=%s">%s</a>' % (base_url, i, i)
page_list.append(temp) if self.current_page == self.total_count:
nex = '<a class="page" href="javascript:void(0);">下一页</a>'
else:
nex = '<a class="page" href="%s?p=%s">下一页</a>' % (base_url, self.current_page + 1,)
page_list.append(nex) jump = """
<input type='text' /><a onclick='jumpTo(this, "%s?p=");'>GO</a>
<script>
function jumpTo(ths,base){
var val = ths.previousSibling.value;
location.href = base + val;
}
</script>
""" % (base_url,) page_list.append(jump) page_str = mark_safe("".join(page_list)) return page_str

分页插件使用手册:

1.在APP同级目录创建 utils目录

 2.在utils目录内创建 pagination.py文件,把上边的代码复制进去

 3.视图函数调用示例。

 def host(request):
if request.method == "GET":
host_list = models.HostInfo.objects.all()
group_list = models.HostGroup.objects.all()
print(">>>",group_list) current_page = request.GET.get('p', 1)
current_page = int(current_page)
val = request.GET.get('per_page_count', 2)
val = int(val)
page_obj = pagination.Page(current_page, len(host_list), val)
data = host_list[page_obj.start:page_obj.end]
page_str = page_obj.page_str("/host")
return render(request,"host.html",{"host_list": data,"group_list":group_list,"page_str":page_str})
elif request.method == "POST":
print("post method")
hostname = request.POST.get("hostname")
hostip = request.POST.get("hostip")
hostpassword = request.POST.get("password")
group_id = request.POST.get("group_id")
models.HostInfo.objects.create(serverip=hostip,servername=hostname,serverpassword=hostpassword,servergroup_id=group_id)
return redirect("/host")

 4.前端html参考添加

     <style>
.pagination .page{
display: inline-block;
padding: 5px;
background-color: cyan;
margin: 5px;
}
.pagination .page.active{
background-color: brown;
color: white;
} </style> </div>
<div style="align:center ;position: fixed;bottom: 3%;left: 40%" class="pagination">
{{ page_str }}
</div>

django之分页插件的更多相关文章

  1. 浅谈Django的Q查询以及AngularJS的Datatables分页插件

    使用Q查询,首先要导入Q模块: from django.db.models import Q 可以组合使用&,|操作符用于多个Q的对象,产生一个新的Q对象,Q对象也可以用~操作符放在前面表示否 ...

  2. Python Django的分页,Form验证,中间件

    本节内容 Django的分页 Form 中间件 1 Django 分页 1.1 Django自带的分页 1.首先来看下我的测试数据环境 ############ models.py ######### ...

  3. django网页分页

    blog/views.py from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger #导入分页插件包 def  ...

  4. Django之CURD插件2

    目标:达到下图拥有功能的实现 1.绑定编辑按钮 ************思路**************** 1.为编辑按钮添加样式,可以根据样式来进行判断在什么状态. 2.进入编辑模式,将可编辑的字 ...

  5. Django使用DataTables插件总结

    Django使用Datatables插件总结 文章中的例子已上传至github 基本使用 Datatables插件是一款方便简单的展示数据的列表插件.关于基本使用,官方网站上的已介绍的很详细,这里我再 ...

  6. MVC如何使用开源分页插件shenniu.pager.js

    最近比较忙,前期忙公司手机端接口项目,各种开发+调试+发布现在几乎上线无问题了:虽然公司项目忙不过在期间抽空做了两件个人觉得有意义的事情,一者使用aspnetcore开发了个人线上项目(要说线上其实只 ...

  7. 分页插件--根据Bootstrap Paginator改写的js插件

    刚刚出来实习,之前实习的公司有一个分页插件,和后端的数据字典约定好了的,基本上是看不到内部是怎么实现的,新公司是做WPF的,好像对于ASP.NET的东西不多,导师扔了一个小系统给我和另一个同事,指了两 ...

  8. [原创]jquery+css3打造一款ajax分页插件

    最近公司的项目将好多分页改成了ajax的前台分页以前写的分页插件就不好用了,遂重写一个 支持IE6+,但没有动画效果如果没有硬需求,个人认为没必要多写js让动画在这些浏览器中实现css3的动画本来就是 ...

  9. 一个强大的jquery分页插件

    点击这里查看效果 这个分页插件使用方便,引用keleyidivpager.js和keleyidivpager.css文件,然后在htm(或者php,aspx,jsp等)页面中对分页总数,参数名,前缀后 ...

随机推荐

  1. DAL 层引用 System.Net.Http ,引发的一阵心慌

    快下班的时候 代码data 数据层编译失败,引起整个解决方案全部失败:其他同事虽然vs 版本不同,但是都能编译通过:考虑到今天更改过vs 的设置,把今天更改的设置全部都恢复,结果还是不行.最后直接恢复 ...

  2. redis和memcache的比较

    1.数据类型支持不同 与Memcached仅支持key-value结构不同,Redis支持的数据类型更丰富,同时支持list.set.hash等数据结构的存储: 2.内存管理不同 在Redis中,并不 ...

  3. channelartlist中autoindex无效的解决方法

    {dede:channelartlist}中有使用autoindex无效的解决方法 在设计频道首页的时候,使用{dede:channelartlist}标签时,有很多朋友想做一些高级的开发,让重复的频 ...

  4. Go中函数作为值、类型传递。

    在Go中函数也是一种变量,我们可以通过type来定义它,它的类型就是所有拥有相同的参数,相同的返回值的一种类型 type typeName func(input1 inputType1 , input ...

  5. 开源:基于Android的室内定位WiFi,iBeacon数据采集和定位脚本

    最近有同学联系我,也在一些群里看到有新手同学挣扎在怎么获取定位数据,不知从何下手.所以整理并开源这个基于Android的数据采集软件和基于python的KNN定位demo,算是为新手同学建立一个Bas ...

  6. tar,jar和war都是什么

    jar 即Java Archive,java的类进行编译生成的class文件,通常是开发时要引用通用类,打成包便于存放管理. 但如果直接发布这些class文件的话会很不方便,所以就把许多的class文 ...

  7. Hyperledger Fabric (1.0)环境部署 chaincode【转】

    三.测试Fabric 其实我们在前面运行./network_setup.sh up的时候系统已经运行了一个Example02的ChainCode测试,部署上去的ChainCodeName是mycc,所 ...

  8. codevs1051接龙游戏

    1051 接龙游戏  

  9. c#根据身份证获取身份证信息

    /// <summary> /// 根据身份证获取身份证信息 /// 18位身份证 /// 0地区代码(1~6位,其中1.2位数为各省级政府的代码,3.4位数为地.市级政府的代码,5.6位 ...

  10. 字符串前面u,r,b

    u :代表是对字符串进行unicode编码. 一般英文字符在使用各种编码下, 基本都可以正常解析, 所以一般不带u:py3当对字符串进行操作的时候,默认使用Unicode编码 r/R:非转义的原始字符 ...