013---Django的分页器
知识预览
分页
Django的分页器(paginator)
view
from django.shortcuts import render, HttpResponse
from app01.models import Book
from django.core.paginator import Paginator, EmptyPage
from app01.utils.pagination import Pagination
def index(request):
# 创建数据
# book_list = [Book(title='Hello Python',price=price) for price in range()]
# res = Book.objects.bulk_create(book_list) # 批量创建,效率高
# print(res)
book_list = Book.objects.all()
# return HttpResponse('ok') paginator = Paginator(book_list, ) # 打印
print('数据总数:', paginator.count) #
print('总页数:', paginator.num_pages) #
print('页码列表:', paginator.page_range) # range(, ) current_page_num = int(request.GET.get('page', ))
if paginator.num_pages > :
if current_page_num - < :
p1 =
p2 =
elif current_page_num + > paginator.num_pages:
p1 = paginator.num_pages -
p2 = paginator.num_pages +
else:
p1 = current_page_num -
p2 = current_page_num +
page_range = range(p1, p2)
else:
page_range = paginator.page_range
try:
current_page = paginator.page(current_page_num) # 第几页的数据
except EmptyPage as e:
current_page = paginator.page()
return render(request, 'book_list.html', locals())

<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>书籍列表</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head>
<body> <ul>
{% for book in current_page %}
<li>名称:{{ book.title }}----价格:{{ book.price }}</li>
<br>
{% endfor %} </ul> <nav aria-label="Page navigation">
<ul class="pagination">
{% if current_page.has_previous %}
<li>
<a href="?page={{ current_page.previous_page_number }}" aria-label="Previous"><span aria-hidden="true">上一页</span></a>
</li>
{% else %}
<li class="disabled">
<a href="" aria-label="Previous"><span aria-hidden="true">上一页</span></a>
</li>
{% endif %} {% for num in page_range %}
{% if current_page_num == num %}
<li class="active"><a href="?page={{ num }}">{{ num }}</a></li>
{% else %}
<li><a href="?page={{ num }}">{{ num }}</a></li>
{% endif %}
{% endfor %}
{% if current_page.has_next %}
<li><a href="?page={{ current_page.next_page_number }}" aria-label="Next"><span
aria-hidden="true">下一页</span></a></li>
{% else %}
<li class="disabled">
<a href="" aria-label="Previous"><span aria-hidden="true">上一页</span></a>
</li>
{% endif %} </ul>
</nav> </body>
</html>
扩展

def index(request):
book_list=Book.objects.all()
paginator = Paginator(book_list, 15)
page = request.GET.get('page',1)
currentPage=int(page)
# 如果页数十分多时,换另外一种显示方式
if paginator.num_pages>30:
if currentPage-5<1:
pageRange=range(1,11)
elif currentPage+5>paginator.num_pages:
pageRange=range(currentPage-5,paginator.num_pages+1)
else:
pageRange=range(currentPage-5,currentPage+5)
else:
pageRange=paginator.page_range
try:
print(page)
book_list = paginator.page(page)
except PageNotAnInteger:
book_list = paginator.page(1)
except EmptyPage:
book_list = paginator.page(paginator.num_pages)
return render(request,"index.html",locals())

自定义分页器

class Pagination(object):
def __init__(self, current_page, all_count, base_url, per_page_num=10, page_count=11):
"""
封装分页相关数据
:param current_page: 当前页
:param all_count: 数据库的数据总条数
:param base_url:分页显示的url前缀
:param per_page_num:每页显示的数据条数
:param page_count:最多显示的页码数
"""
try:
current_page = int(current_page)
except Exception as e:
# 当输入的页码不是正经数字的时候,默认返回第一页数据
current_page = 1
if current_page < 1:
current_page = 1
self.current_page = current_page
self.all_count = all_count
self.base_url = base_url
self.per_page_num = per_page_num
self.page_count = page_count
self.page_count_half = int(page_count) // 2
# 总页码
all_page, tmp = divmod(all_count, per_page_num) # 7,6 = div(90,12)
# 如果有多余,加一页:
if tmp:
all_page += 1
self.all_page = all_page
@property
def start(self):
# 从哪开始
return (self.current_page - 1) * self.per_page_num
@property
def end(self):
# 到哪结束
return self.current_page * self.per_page_num
def page_html(self):
# 如果总页码 <= 11:
if self.all_page <= self.page_count:
page_start = 1
page_end = self.all_page + 1
# 如果页码数 > 11:
else:
# 如果当前页 <= self.page_count_half
if self.current_page <= self.page_count_half:
page_start = 1
page_end = self.page_count + 1
# 当前页大于5
else:
# 页码翻到最后
if (self.current_page + self.page_count_half) > self.all_page:
page_start = self.all_page - self.page_count + 1
page_end = self.all_page + 1
else:
page_start = self.current_page - self.page_count_half
page_end = self.current_page + self.page_count_half + 1
page_html_list = []
first_page = '<li><a href ="%s?page=%s">首页</a></li>' % (self.base_url, 1)
page_html_list.append(first_page)
if self.current_page <= 1:
prev_page = '<li class="disabled"><a href="#">上一页</a></li>'
else:
prev_page = '<li><a href="%s?page=%s">上一页</a></li>' % (self.base_url, self.current_page - 1,)
page_html_list.append(prev_page)
for i in range(page_start, page_end):
if i == self.current_page:
temp = '<li class="active"><a href="%s?page=%s">%s</a></li>' % (self.base_url, i, i,)
else:
temp = '<li><a href="%s?page=%s">%s</a></li>' % (self.base_url, i, i,)
page_html_list.append(temp)
if self.current_page >= self.all_page:
next_page = '<li class="disabled"><a href="#">下一页</a></li>'
else:
next_page = '<li><a href="%s?page=%s">下一页</a></li>' % (self.base_url, self.current_page + 1,)
page_html_list.append(next_page)
last_page = '<li><a href="%s?page=%s">尾页</a></li>' % (self.base_url, self.all_page,)
page_html_list.append(last_page)
return ''.join(page_html_list)

使用案例:
def index1(request):
book_list = Book.objects.all()
current_page = request.GET.get('page',)
obj = Pagination(current_page,len(book_list),request.path)
page_book_list = book_list[obj.start:obj.end]
print(page_book_list)
page_html = obj.page_html()
return render(request,'book_list1.html',locals())
<!DOCTYPE html>
<html lang="zh_CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body> <ul>
{% for book in page_book_list %}
<li>名称:{{ book.title }}----价格:{{ book.price }}</li>
<br>
{% endfor %} </ul> <nav aria-label="Page navigation">
<ul class="pagination">
{{ page_html|safe }}
</ul>
</nav>
</body>
</html>
013---Django的分页器的更多相关文章
- 使用Django实现分页器功能
要使用Django实现分页器,必须从Django中导入Paginator模块 from django.core.paginator import Paginator 假如现在有150条记录要显示,每页 ...
- 【django之分页器】
一.什么是分页功能 二.Django的分页器(paginator) 语法: paginator = Paginator(book_list, 8) #8条一页print("count:&qu ...
- django自定义分页器
一 django 的分页器 1 批量创建数据 批量导入数据: Booklist=[] for i in range(100): Booklist.append(Book(title="boo ...
- Django组件-分页器
Django的分页器(paginator) view from django.shortcuts import render,HttpResponse # Create your views here ...
- Django框架----分页器(paginator)
Django的分页器(paginator) view.py from django.shortcuts import render,HttpResponse # Create your views h ...
- django的分页器
Django中分页器的使用 django分页器模块 #分页器 from django.core.paginator import Paginator,EmptyPage,PageNotAnIntege ...
- Django组件(一) Django之分页器
Django的分页器(paginator)简介 在页面显示分页数据,需要用到Django分页器组件 from django.core.paginator import Paginator Pagina ...
- Django 组件-分页器
Django的分页器(paginator) view from django.shortcuts import render,HttpResponse # Create your views here ...
- Django - 文件上传、Django组件 - 分页器(paginator)
一.文件上传准备知识 - Content-Type 1.请求头 - Content-Type Content-Type指的是请求体的编码类型,常见的类型共有3种: 1)application/x-ww ...
- Django 进阶(分页器&中间件)
分页 Django的分页器(paginator) view from django.shortcuts import render,HttpResponse # Create your views h ...
随机推荐
- 【Android学习入门】Android studio基本设置
1.背景设置 依次选择File->Settings-->Appearance & Behaviour->Apprearance,然后勾选 show line number. ...
- 给大家推荐一个.Net的混淆防反编译工具ConfuserEx
给大家推荐一个.Net的混淆防反编译工具ConfuserEx. 由于项目中要用到.Net的混淆防反编译工具. 在网上找了很多.Net混淆或混淆防反编译工具,如.NET Reactor.Dotfusca ...
- 线程队列queue
队列queue 队列用于线程之间安全的信息交换 队列和列表的区别:队列里的信息get()后就没了,而列表获取数据则是copy,原列表里的值还在 使用前先实例化队列 q = queue.Queue(ma ...
- 解决dubbo-admin管控台不能显示服务的问题
1.首先在网上下载了dubbo-admin.war,解压后修改dubbo.properties文件 dubbo.registry.address=zookeeper://127.0.0.1:2181 ...
- Azure进阶攻略 | VS2015和Azure,想要在一起其实很容易
下雨天,巧克力和音乐很配…… 大冬天,男神和捧在手里的奶茶很配…… 「驴牌」的包包,和女神的全部衣服都配…… 对于「王首富」,容易实现的小目标和一个亿是绝配…… …… 醒醒吧!!这些事情和每天只会写代 ...
- Java Map应用
一.基本API使用方法 直接上代码,注释讲解 package com.map; import java.util.HashMap; import java.util.Iterator; import ...
- ASP.NET写入和读取xml文件
xml是一种可扩展标记语言,在电子计算机中,标记指计算机所能理解的信息符号,通过此种标记,计算机之间可以处理包含各种的信息比如文章等.它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进 ...
- CentOS6下DHCP服务(二)简单配置案例及故障排查
1.预分配网络参数如下:linux服务器:eth0 IP为192.168.8.250 做为局域网DHCP服务器局域网网段设置为192.168.8.0/24:内部计算机的router为192.168. ...
- COGS 2075. [ZLXOI2015][异次元圣战III]ZLX的陨落
★★☆ 输入文件:ThefallingofZLX.in 输出文件:ThefallingofZLX.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] 正当革命如火如 ...
- 【JavaScript 封装库】BETA 1.0 测试版发布!
/* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =============================================== ...