前言:当数据很多时,需要把数据分页展示,就需要用到分页
1.分页的基本逻辑
1.1 分页会用到的基本元素
- 每页数据的条数 pre_page
- 当前页码 page
students = Students.objects.filter(is_delete=False)
students是一个列表,可以用切片的方式分页传送数据
如果每页是展示10条数据,可以用下面的切片方式
第一页 [0:10]
第二页[10:20]
第三页[20:30]
泛化成公式 切片的公式如下:
star = (page-1)*pre_page
end = page*pre_page
students = students[ (page-1)*pre_page:page*pre_page]
page 和 pre_page 就是分页必须用到的两个参数,page是当前页码数,pre_page 是每页输出多少条数据
在 view 视图函数中这样写就能实现分页输出
def student_list_views(request):
students = Students.objects.filter(is_delete=False)
students_count = students.count()
page = int(request.GET.get('page')) if request.GET.get('page') else 1
pre_page = int(request.GET.get('pre_page')) if request.GET.get('pre_page') else 10
students = students[(page-1)*pre_page:page*pre_page] #将students 切片 分页输出
total_page = math.ceil(students_count/pre_page) #用ceil函数计算出总页数
student_name = request.GET.get('name',None)
if student_name:
students = students.filter(name=student_name)
return render(request, 'teacher/student_list.html', locals())
return render(request,'teacher/student_list.html',locals())
1.2 用自定义包含标签实现一个分页
用自定义包含标签实现一个完整的分页功能
先在templatetags
文件夹中定义一个包含标签 paging.py
from django.template import Library
register = Library()
@register.inclusion_tag("teacher/paging.html")
def paging (page,pre_page,total_page):
"""
param:
page:当前页码数
pre_page: 每页显示多少条数据
total_page:总共多少页码
return:
page_list:显示当前所有页码的列表
"""
page_list = []
page_num = 5 #总共显示多少个页码
if total_page > page_num: #当总共大于5个页码时
#计算当前页码左边有哪些页码
if (page - ((page_num - 1) // 2)) > 0: #当最左边的页码不是 1 时,左边所有的页码
for i in range((page - ((page_num - 1) // 2)), page + 1):
page_list.append(i)
else:
for i in range(1, page + 1): #当最左边的页码是 1 时,左边的页码
page_list.append(i)
# 计算当前页码右边有哪些页码
if (page + (page_num - 1) // 2) >= total_page: #当最右边的页码不是最后一个页码时,右边所有的页码
for i in range(page + 1, total_page + 1):
page_list.append(i)
else:
for i in range(page + 1, page + ((page_num - 1) // 2) + 1): #当最右边的页码是最后一个页码时,右边所有的页码
page_list.append(i)
else:
for i in range(1, total_page + 1):
page_list.append(i)
print(page_list)
return {'page':page,
'pre_page':pre_page,
'total_page':total_page,
'page_list':page_list
}
然后再自定义一个模板文件paging.html
<nav aria-label="Page navigation">
<ul class="pagination" style="margin: 0px">
{#当前页面为第一页时禁用上一页标签#}
<li {% if page == 1 %}class="disabled" {% endif %}>
{% if page > 1 %}
<a href="{% url "teacher:studentList" %}?page={{ page|add:-1 }}&pre_page={{ pre_page }}" aria-label="Previous">
{% endif %}
<span aria-hidden="true">«</span>
</a>
</li>
{% for item in page_list %}
<li class="{% if page == item %}active{% endif %}"><a href="{% url "teacher:studentList" %}?page={{ item }}&pre_page={{ pre_page }}">{{ item }}</a></li>
{% endfor %}
{#当前页面为最后一页时禁用下一页标签#}
<li {% if page == total_page %}class="disabled"{% endif %}>
{% if page < total_page %}
<a href="{% url "teacher:studentList" %}?page={{ page|add:1 }}&pre_page={{ pre_page }}" aria-label="Next">
{% endif %}
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
在模板文件中使用自定义包含标签paging
<div class="btn-group">
{% paging page pre_page total_page %}
</div>
2. Django中的分页类
Django提供了一系列的类帮助你管理分页数据,这些类位于
django/core/paginator.py
演示
root@pts/0 # python
Python 3.5.2 (default, Aug 8 2017, 15:35:13)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from django.core.paginator import Paginator
## 作为测试分页的对象
>>> objects = ['post1', 'post2', 'post3', 'post4', 'post5', 'post6', 'post7']
## 期望按照每页三个对象来实现分页
>>> paginator = Paginator(objects, 3)
## 输入paginator. 然后连续按两次 tab 键,展示 paginator 对应的方法属性等
>>> paginator.
paginator.__class__( paginator.__le__( paginator.__weakref__
paginator.__delattr__( paginator.__lt__( paginator._check_object_list_is_ordered(
paginator.__dict__ paginator.__module__ paginator._get_page(
paginator.__dir__( paginator.__ne__( paginator.allow_empty_first_page
paginator.__doc__ paginator.__new__( paginator.count
paginator.__eq__( paginator.__reduce__( paginator.num_pages
paginator.__format__( paginator.__reduce_ex__( paginator.object_list
paginator.__ge__( paginator.__repr__( paginator.orphans
paginator.__getattribute__( paginator.__setattr__( paginator.page(
paginator.__gt__( paginator.__sizeof__( paginator.page_range
paginator.__hash__( paginator.__str__( paginator.per_page
paginator.__init__( paginator.__subclasshook__( paginator.validate_number(
## 要分页的总对象数
>>> paginator.count
7
>>> paginator.object_list
['post1', 'post2', 'post3', 'post4', 'post5', 'post6', 'post7']
## 总共可以分页数,也就是总共可以分成多少页
>>> paginator.num_pages
3
## 页码取值范围
>>> paginator.page_range
range(1, 4)
## 每页几个对象
>>> paginator.per_page
3
## 取第2页, 或者第N页`paginator.page(N)`
>>> page2 = paginator.page(2)
## page2 的属性和方法
>>> page2.
page2.__abstractmethods__ page2.__le__( page2._abc_cache
page2.__class__( page2.__len__( page2._abc_negative_cache
page2.__contains__( page2.__lt__( page2._abc_negative_cache_version
page2.__delattr__( page2.__module__ page2._abc_registry
page2.__dict__ page2.__ne__( page2.count(
page2.__dir__( page2.__new__( page2.end_index(
page2.__doc__ page2.__reduce__( page2.has_next(
page2.__eq__( page2.__reduce_ex__( page2.has_other_pages(
page2.__format__( page2.__repr__( page2.has_previous(
page2.__ge__( page2.__reversed__( page2.index(
page2.__getattribute__( page2.__setattr__( page2.next_page_number(
page2.__getitem__( page2.__sizeof__( page2.number
page2.__gt__( page2.__slots__ page2.object_list
page2.__hash__( page2.__str__( page2.paginator
page2.__init__( page2.__subclasshook__( page2.previous_page_number(
page2.__iter__( page2.__weakref__ page2.start_index(
## 第2页含有的对象列表
>>> page2.object_list
['post4', 'post5', 'post6']
## 从最上面我们知道,总共有7个对象,每页三个对象,我们总共分成3个页面,第二个页面在中间
## 所有第二页有next和previous, 并且可以得到Next 和 previous 的页码值
>>> page2.has_next()
True
>>> page2.next_page_number()
3
>>> page2.has_previous()
True
>>> page2.previous_page_number()
1
>>> page2.has_other_pages()
True
## 第二页开始和结束的对象索引
>>> page2.start_index()
4
>>> page2.end_index()
6
>>>
## 针对该测试例子。我们知道0和4页肯定是不存在, 所以出现 emptypage 异常
## 所以一般页面不存在或者页面无效之类的异常。我们都会让其跳转到第一页
>>> paginator.page(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/root/.pyenv/versions/blog/lib/python3.5/site-packages/django/core/paginator.py", line 57, in page
number = self.validate_number(number)
File "/root/.pyenv/versions/blog/lib/python3.5/site-packages/django/core/paginator.py", line 45, in validate_number
raise EmptyPage(_('That page number is less than 1'))
django.core.paginator.EmptyPage: <exception str() failed>
>>> paginator.page(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/root/.pyenv/versions/blog/lib/python3.5/site-packages/django/core/paginator.py", line 57, in page
number = self.validate_number(number)
File "/root/.pyenv/versions/blog/lib/python3.5/site-packages/django/core/paginator.py", line 50, in validate_number
raise EmptyPage(_('That page contains no results'))
django.core.paginator.EmptyPage: <exception str() failed>
>>>
网友评论