知识预览

分页

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的分页器的更多相关文章

  1. 使用Django实现分页器功能

    要使用Django实现分页器,必须从Django中导入Paginator模块 from django.core.paginator import Paginator 假如现在有150条记录要显示,每页 ...

  2. 【django之分页器】

    一.什么是分页功能 二.Django的分页器(paginator) 语法: paginator = Paginator(book_list, 8) #8条一页print("count:&qu ...

  3. django自定义分页器

    一 django 的分页器 1 批量创建数据 批量导入数据: Booklist=[] for i in range(100): Booklist.append(Book(title="boo ...

  4. Django组件-分页器

    Django的分页器(paginator) view from django.shortcuts import render,HttpResponse # Create your views here ...

  5. Django框架----分页器(paginator)

    Django的分页器(paginator) view.py from django.shortcuts import render,HttpResponse # Create your views h ...

  6. django的分页器

    Django中分页器的使用 django分页器模块 #分页器 from django.core.paginator import Paginator,EmptyPage,PageNotAnIntege ...

  7. Django组件(一) Django之分页器

    Django的分页器(paginator)简介 在页面显示分页数据,需要用到Django分页器组件 from django.core.paginator import Paginator Pagina ...

  8. Django 组件-分页器

    Django的分页器(paginator) view from django.shortcuts import render,HttpResponse # Create your views here ...

  9. Django - 文件上传、Django组件 - 分页器(paginator)

    一.文件上传准备知识 - Content-Type 1.请求头 - Content-Type Content-Type指的是请求体的编码类型,常见的类型共有3种: 1)application/x-ww ...

  10. Django 进阶(分页器&中间件)

    分页 Django的分页器(paginator) view from django.shortcuts import render,HttpResponse # Create your views h ...

随机推荐

  1. Python用户交互以及数据类型

    一.用户交互与格式化输出 1.用户交互 1.1什么是用户交互 程序等待用户输入的数据,程序执行完毕后为用户反馈信息. 1.2为何程序要与用户交互 为了让计算机像人类一样与用户交互 1.3使用方式 在p ...

  2. Wallet address

    BCX XZVYYwXFAJwv6x4KTssQxJb4EReVdCBnpb BCD 1DNSFUD7LURZdmbckkQcxMvinNJ26mVcNH

  3. u-boot分析(三)---boot命令实现以及内核的启动

    上片博文总结出了u-boot的工作流程,今天我们来分析,u-boot的两个比较重要的内容 1.        U-boot命令的实现 2.        U-boot如何启动内核 l  命令实现 我们 ...

  4. 二叉查找树(c++)

    二叉查找数的操作: #include <iostream> using namespace std; typedef struct BitNode { int data; struct B ...

  5. helm深入学习

    Helm把Kubernetes资源(比如deployments.services或 ingress等) 打包到一个chart中,而chart被保存到chart仓库.通过chart仓库可用来存储和分享c ...

  6. Win10技巧:使用“照片”应用剪辑视频、添加特效

    Win10内置了很多实用的应用,你不仅可以通过“Win键+G”快速录制电脑屏幕,如软件操作.游戏界面等,你还可以利用“照片”应用来对视频进行快速的剪辑,把录制前后多余的内容去除,同时你也可以对游戏中的 ...

  7. 【CSS古话今说】-- 01.神奇的CSS-BFC在实战中的应用

    文章首发于掘金 BFC(Block Formatting Context)是Web页面中盒模型布局的CSS渲染模式.它的定位体系属于常规文档流. 想要实现一个BFC布局需要满足以下条件之一: 1.fl ...

  8. Selenium入门8 js调用

    execute_script 可以执行js和jquery脚本 示例如下:修改百度首页的按钮字体颜色,按钮隐藏,按钮显示 #coding=utf-8 # 调用js jquery from seleniu ...

  9. Java中调用MatLab返回值

    当在Java中使用MatLab函数时,由于语言语法的不同,Matlab返回多个数据时,想在Java中获取到并进行使用.查阅了网上资料,翻箱倒柜加上自己实战,得出方法如下: 如MatLab函数返回的是N ...

  10. CentOS 5 - 安装PHP MongoDB扩展

    For driver developers and people interested in the latest bugfixes, you can compile the driver from ...