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. C++与UnrealScript脚本交互

    转自:http://m.blog.csdn.net/blog/qweewqpkn/39932499 一.c++调用uc脚本中的函数 举例: 1. 在脚本MenuManager.uc文件中实现函数: e ...

  2. linux 命令2

    who who am i ssh scott@192.168.1.105 ps aux | grep pts/8 pwd // where are you? Page 205 mkdir -p dir ...

  3. java---集合类(1)

    java.util包中包含了一系列重要的集合类.而对于集合类,主要需要掌握的就是它的内部结构,以及遍历集合的迭代模式. 接口:Collection Collection是最基本的集合接口,一个Coll ...

  4. html中连续点击某个标签会出现蓝色的解决方法

    给标签加上下面的属性就可以了,也可以把这些属性建立一个class名,谁需要的时候加上也ok -moz-user-select: none; /*mozilar*/ -webkit-user-selec ...

  5. [poj1222]EXTENDED LIGHTS OUT(高斯消元)

    题意:每个灯开启会使自身和周围的灯反转,要使全图的灯灭掉,判断灯开的位置. 解题关键:二进制高斯消元模板题. 复杂度:$O({n^3})$ #include<cstdio> #includ ...

  6. 5、opencv中的绘图函数

    1.目标 a.学习使用 OpenCV 绘制不同几何图形 b. 你将会学习到这些函数: cv2.line(), cv2.circle(), cv2.rectangle(),cv2.ellipse(),c ...

  7. NOIP2015提高组 跳石头 ACM-ICPC2017香港 E(选择/移除+二分答案)

    跳石头 题目背景 一年一度的“跳石头”比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间,有 NN  ...

  8. Linux 基础命令(一)

    Linux 基础: https://www.cnblogs.com/linhaifeng/articles/6045600.html Linux 比 Windows 更稳定做服务器,开发出来的软件需要 ...

  9. SCUT - 336 - 酋雷姆 - 最小生成树

    每个世界可以和别的世界连通,也可以直接联通虚拟的已经毁灭的世界,这样变成一个最小生成树问题. 但是好像哪里不对? 有人用dp过掉的? 不太清楚怎么搞的. 其实就是最小生成树-- #include< ...

  10. Jmeter接口自动化参数化 (转自软件测试部落)

    测试场景: 有个查询城市(大概一百个 )天气预报的接口(需求参考第一课),需要根据不同的citycode,去查询对应城市的天气预报,这种接口该如何去测试呢? 分析需求: 不管是功能测试需求,还是接口测 ...