Python自定义-分页器
分页功能在每个网站都是必要的,对于分页来说,其实就是根据用户的输入计算出应该在数据库表中的起始位置。
1、设定每页显示数据条数
2、用户输入页码(第一页、第二页...)
3、根据设定的每页显示条数和当前页码,计算出需要取数据表的起始位置
4、在数据表中根据起始位置取值,页面上输出数据
通过bootcss实现分页图标样式
1.模板页引入bootcss 插件文件,定制显示样式
<head>
{# 项目目录 需要 下载bootstrap相关插件 #}
<link rel="stylesheet" href="/static/dist/css/bootstrap.css">
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js "></script>
<script src="/static/dist/js/bootstrap.js"></script>
<style>
.pager a{
display: inline-block;
padding: 3px 5px;
margin: 0 3px;
border: 1px solid #dddddd;
}
.pager a.active{
background-color: cadetblue;
color: white;
}
</style>
</head>
<body>
<table>
<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">
<ul class="pagination" id="pager">
{{ page_html}}
</ul>
</div>
</body>
2.settings.py 设置静态文件的路径
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
3.制作分页器模板
模板位置
# mark_safe 是将 html 格式代码传到 前端时,在页面可以直接渲染
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 total_count: 数据库中查询到的数据总条数
:param base_url: 请求的 url 路径
:param per_page_count: 每页显示的数据条数
:param max_pager_num: 页面上最多显示的页码
"""
# url 地址 “/host/”
self.base_url = base_url
# divmod(total_count, per_page_count) 总条数/每页显示条数 得到得 (商,余) 赋值给 total_page_count总页数, div余值
total_page_count, div = divmod(total_count, per_page_count)
if div:
# 如果 div 有值 总页数 +1
total_page_count += 1
# total_page_count 总页数
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):
# 开始显示id 值 为 (当前页-1)*每页显示条数
return (self.current_page - 1) * self.per_page_count
@property
def end(self):
# 结束显示id 值 为 当前页*每页显示条数
return self.current_page * self.per_page_count
# html 具体显示内容
def page_html(self):
page_html_list = []
# 首页
head_page = "<li><a href='%s?page=%s'>首页</a></li>" %(self.base_url, 1)
page_html_list.append(head_page)
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><a class='active' 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)
# 尾页
tail_page = "<li><a href='%s?page=%s'>尾页</a></li>" % (self.base_url, self.total_page_count)
page_html_list.append(tail_page)
# 将拼接的页码数据 join 序列化 传到 模板
# print("".join(page_html_list))
return mark_safe("".join(page_html_list))
4.在视图函数中引用utils.pager 自定义模板
# 导入 通过 Pagination 封装的 分页功能 包
from utils.pager import Pagination
def host(request):
# 统计出一共有多少条 数据
all_count = models.Host.objects.all().count()
# page_obj = Pagination 方法需要 3个 参数(请求当前页码,总数据条数,该应用路径)
# 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()[本页内容的开始,本页内容的结束]
host_list = models.Host.objects.all()[page_obj.start:page_obj.end]
# render(request, 页面, {展示内容,展示的 html页码})
return render(request, 'host.html', {'host_list': host_list, 'page_html': page_obj.page_html()})