知识预览

分页

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. echarts自适应宽度

    const myChartContainer = document.getElementById( id ); const resizeMyChartContainer = function () { ...

  2. 阿里前端笔试总结--H5面试题

    转载网址 https://blog.csdn.net/qq_20913021/article/details/51351801 1.有一个长度未知的数组a,如果它的长度为0就把数字1添加到数组里面,否 ...

  3. Python is 和 == 的区别, 编码和解码

    一.is 和 == 的区别 is : 进行比较,比较的是内存地址是否一致 ==:进行比较,比较的是值是否相等 1.小数据池: 数字小数据池范围 -5~256 字符串中如果有特殊字符则他们的内存地址不一 ...

  4. 修改Android系统关机动画

    文件路径:frameworks\base\services\core\java\com\android\server\power\ShutdownThread.java 在beginShutdownS ...

  5. 【起航计划 026】2015 起航计划 Android APIDemo的魔鬼步伐 25 App->Notification->Status Bar 状态栏显示自定义的通知布局,省却声音、震动

    这个例子的Icons Only 和 Icons and marquee 没有什么特别好说明的. 而Use Remote views in balloon 介绍了可以自定义在Extended Statu ...

  6. 【Android 界面效果47】RecyclerView详解

    RecylerView作为 support-library发布出来,这对开发者来说绝对是个好消息.因为可以在更低的Android版本上使用这个新视图.下面我们看如何获取 RecylerView.首先打 ...

  7. PhoneGap&jQuery Mobile应用开发环境配置(For Android)

    关于移动应用为什么用PhoneGap和jQuery Mobile本文不再赘述,有兴趣的童鞋可以自行问“度娘”,有很多这方面的文章.本文主要介绍PhoneGap&jQuery Mobile移动应 ...

  8. Eclipse:很不错的插件-devStyle,将你的eclipse变成idea风格

    使用教程  https://blog.csdn.net/stillonmyway/article/details/79109741 我使用使用的是护眼型的

  9. TP5.1:依赖注入、绑定一个类到容器里、绑定一个闭包到容器中

    依赖注入 1.在application中创建一个文件夹,名字为commom,commom文件夹中创建被注入文件夹,在被注入文件夹中创建一个名为demo.php的文件 2.在demo.php中输入: 3 ...

  10. 双击易语言没有反应,按住shift再双击可解决

    参考资料:http://tieba.baidu.com/p/2987732743 的7楼.