1.准备数据

在models创建测试表

from django.db import models

class Host(models.Model):
hostname = models.CharField(max_length=32)
# ip = models.GenericIPAddressField(protocol='ipv4')
ip = models.CharField(max_length=32)
port = models.IntegerField()

2.自动化生成数据

    hostname = "hostname%s.com"
for i in range(302):
models.Host.objects.create(hostname=hostname %str(i),ip="1.1.1.1",port=8080)
return HttpResponse("...")

3.分页组件

"""
分页组件:
使用方法:
视图函数:
from utils.pager import Pagination
def host(request):
all_count = models.Host.objects.all().count()
# page_obj = Pagination(request.GET.get('page'),all_count,'/host/')
page_obj = Pagination(request.GET.get('page'),all_count,request.path_info)
host_list = models.Host.objects.all()[page_obj.start:page_obj.end]
return render(request,'host.html',{'host_list':host_list,'page_html': page_obj.page_html()})
HTML:
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>
.pager .active>a {
z-index: 3;
color: #fff;
cursor: default;
background-color: #337ab7;
border-color: #337ab7;
} .pager li>a:hover {
z-index: 5;
color: #fff;
cursor: default;
background-color: #337ab7;
border-color: #337ab7;
} .pager li>a{
border-radius: 5px;
margin: 0 3px;
padding: 3px 5px;
min-width:40px;
} </style>
""" from django.utils.safestring import mark_safe
class Pagination(object):
def __init__(self,current_page,total_count,base_url, per_page_count=10,max_pager_num=11):
"""
:param current_page: 用户请求的当前页
:param per_page_count: 每页显示的数据条数
:param total_count: 数据库中查询到的数据总条数
:param max_pager_num: 页面上最多显示的页码
"""
self.base_url = base_url
total_page_count, div = divmod(total_count, per_page_count)
if div:
total_page_count += 1 self.total_page_count = total_page_count
try:
current_page = int(current_page)
except Exception as e:
current_page = 1
if current_page > total_page_count:
current_page = total_page_count self.current_page = current_page
self.per_page_count = per_page_count
self.total_count = total_count
self.max_pager_num = max_pager_num
self.half_max_pager_num = int(max_pager_num/2) @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 def page_html(self):
page_html_list = [] page_html_list.append("<ul>")
if self.current_page <= 1:
prev = "<li><a href='#'>上一页</a></li>"
else:
prev = "<li><a href='%s?page=%s'>上一页</a></li>" % (self.base_url,self.current_page - 1,)
page_html_list.append(prev) max_pager_num = 11
half_max_pager_num = int(max_pager_num / 2) # 数据总页数 < 页面上最大显示的页码个数
if self.total_page_count <= max_pager_num:
page_start = 1
page_end = self.total_page_count
else:
# 数据比较多,已经超过11个页码
# 如果当前页 <=5,显示 1-11
if self.current_page <= half_max_pager_num:
page_start = 1
page_end = max_pager_num
else:
# 当前页 >= 6
if (self.current_page + 5) > self.total_page_count:
page_end = self.total_page_count
# page_start = current_page - 5
page_start = self.total_page_count - max_pager_num + 1
else:
page_start = self.current_page - half_max_pager_num # 当前页 - 5
page_end = self.current_page + half_max_pager_num # 当前页 + 5 for i in range(page_start, page_end + 1):
if self.current_page == i:
tag = "<li class='active'><a href='%s?page=%s'>%s</a></li>" % (self.base_url,i, i,)
else:
tag = "<li><a href='%s?page=%s'>%s</a></li>" % (self.base_url,i, i,)
page_html_list.append(tag) # 下一页
if self.current_page >= self.total_page_count:
nex = "<li><a href='#'>下一页</a></li>"
else:
nex = "<li><a href='%s?page=%s'>下一页</a></li>" % (self.base_url,self.current_page + 1,)
page_html_list.append(nex)
page_html_list.append("</ul>") return mark_safe("".join(page_html_list))

4.页面代码

{% block css %}
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>
.pager .active>a {
z-index: 3;
color: #fff;
cursor: default;
background-color: #337ab7;
border-color: #337ab7;
}
.pager li>a:hover {
z-index: 5;
color: #fff;
cursor: default;
background-color: #337ab7;
border-color: #337ab7;
}
.pager li>a{
border-radius: 5px;
margin: 0 3px;
padding: 3px 5px;
min-width:40px;
}
</style>
{% endblock %} {% block body %}
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>主机名</th>
<th>IP</th>
<th>端口</th>
</tr>
</thead>
<tbody>
{% for row in host_list %}
<tr>
<td>{{ row.id }}</td>
<td>{{ row.hostname }}</td>
<td>{{ row.ip }}</td>
<td>{{ row.port }}</td>
</tr>
{% endfor %}
</tbody>
</table> <div class="pager">
{{ page_html}}
</div>
</div> {% endblock %}

5.展示效果

django自定义分页控件的更多相关文章

  1. asp.net webform 自定义分页控件

    做web开发一直用到分页控件,自己也动手实现了个,使用用户自定义控件. 翻页后数据加载使用委托,将具体实现放在在使用分页控件的页面进行注册. 有图有真相,给个直观的认识: 自定义分页控件前台代码: & ...

  2. C# DataGridView自定义分页控件

    好些日子不仔细写C#代码了,现在主要是Java项目,C#.Net相关项目不多了,有点手生了,以下代码不足之处望各位提出建议和批评. 近日闲来无事想研究一下自定义控件,虽然之前也看过,那也仅限于皮毛,粗 ...

  3. Mvc自定义分页控件

    MVC开发分页常常使用第三方控件,生成的分页HTML带有版权申明,虽然免费,但是总有的别扭.于是,某日,楼主闲来蛋疼,折腾了个自定义分页控件: 先来展示下效果图: 1>当分页不超过10页的时候, ...

  4. WPF自定义分页控件,样式自定义,简单易用

    WPF自定义分页控件 做了许久伸手党,终于有机会贡献一波,搜索一下WPF分页控件,还是多,但是不太通用,主要就是样式问题,这个WPF很好解决,还有一个就是分页控件嘛,只关心几个数字的变动就行了,把页码 ...

  5. Winform自定义分页控件的实现

    实现效果 有点丑陋 但是功能是没问题的 测试过 实现思路 先创建一个用户控件 代码实现 public partial class PagerControl : UserControl { ; /// ...

  6. winform 自定义分页控件 及DataGridview数据绑定

    分页效果如上图所示,用到的控件均为基本控件 ,其方法如下 右击项目-添加-新建项 选择用户控件 然后在用户控件中拖入所需要的Label,Button,Text 用户控件全部代码: using Syst ...

  7. WPF管理系统自定义分页控件 - WPF特工队内部资料

    最近做一个演示的管理系统项目,需要用到分页控件,在网上找了很多,依然找到与UI模版匹配的,最后干脆自己写一个. 分页控件分析: 1.分页控件分简单显示和复杂显示两种: 2.包含上一页.下一页以及页码明 ...

  8. WPF 自定义分页控件二

    一:添加自定义分页控件,命名为KDataPagerTwo: public class KDataPagerTwo : Control, INotifyPropertyChanged { static ...

  9. 自定义分页控件-基于Zhifeiya的分页控件改版

    基于Zhifeiya的分页控件改版的分页. html显示代码: <div class="pagelist"> {{.pagerHtml}} </div> c ...

随机推荐

  1. 吐泡泡(2018年全国多校算法寒假训练营练习比赛(第二场)+栈模拟)+Plug-in(codeforces81A+栈模拟)

    吐泡泡题目链接:https://www.nowcoder.com/acm/contest/74/A 题目: 思路: 这种题目当初卡了我很久,今天早训时遇到一个一样得题,一眼就想到用栈模拟,就又回来把这 ...

  2. 天梯赛 L2-009 抢红包

    题目链接 没有人没抢过红包吧-- 这里给出N个人之间互相发红包.抢红包的记录,请你统计一下他们抢红包的收获. 输入格式: 输入第一行给出一个正整数N(<= 104),即参与发红包和抢红包的总人数 ...

  3. 图片轮播器——jquery插件

    下载:http://files.cnblogs.com/files/wordblog/jiaoben828.rar

  4. Web安全的三个攻防姿势

    原文地址:https://segmentfault.com/a/1190000011601837 作者: zwwill_木羽 关于Web安全的问题,是一个老生常谈的问题,作为离用户最近的一层,我们大前 ...

  5. SQLite3使用详解

    sqlite常量的定义(SQLite3返回值的意思): SQLITE_OK           = 0;  返回成功 SQLITE_ERROR        = 1;  SQL错误或错误的数据库 SQ ...

  6. LeetCode解题报告—— Best Time to Buy and Sell Stock

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

  7. 理解 pkg-config 工具(linux编译辅助工具)

    转:http://www.jb51.net/LINUXjishu/86519.html 你在 Unix 或 Linux 下开发过软件吗?写完一个程序,编译运行完全正常,在你本机上工作得好好的,你放到源 ...

  8. 冒泡法的算法最佳情况下的时间复杂度为什么是O(n)

    我在许多书本上看到冒泡排序的最佳时间复杂度是O(n),即是在序列本来就是正序的情况下. 但我一直不明白这是怎么算出来的,因此通过阅读<算法导论-第2版>的2.2节,使用对插入排序最佳时间复 ...

  9. 全栈Python 必备库

    强大的库: 转自:微信公众号 Python最棒的地方之一,就是大量的第三方库,覆盖之广,令人惊叹.Python 库有一个缺陷就是默认会进行全局安装.为了使每个项目都有一个独立的环境,需要使用工具vir ...

  10. 学习build-web-application-with-golang第四章内容

    GITHUB网址: https://github.com/astaxie/build-web-application-with-golang 内容 4.表单 4.1 处理表单的输入 4.2 验证表单的 ...