目录
- Django 分页器
- 方法介绍
- 示例
- 分页器模板
Django 分页器
文章图片
在页面显示分页数据,需要用到Django分页器组件先看效果图:
【Django|Django 分页器】
文章图片
使用分页器需要导入模块
- 导入:
from django.core.paginator import Paginator
Paginator对象:
paginator = Paginator(user_list, 10)# 传入分页数据,和展示的条数
# per_page: 每页显示条目数量
# count:数据总个数
# num_pages:总页数
# page_range:总页数的索引范围,如: (1,10),(1,200)
# page:page对象page对象:page=paginator.page(1)
# has_next是否有下一页
# next_page_number下一页页码
# has_previous是否有上一页
# previous_page_number上一页页码
# object_list分页之后的数据列表
# number当前页
# paginatorpaginator对象
示例
'''urls.py'''
from django.contrib import admin
from django.urls import path
from app01 import viewsurlpatterns = [
path('admin/', admin.site.urls),
path('user_list/',views.user_page),
path('delete/',views.delete),
]
'''views.py'''
from django.core.paginator import Paginator
from django.shortcuts import render, redirectfrom app01 import modelsdef user_page(request):
user_page_num = int(request.GET.get('page_num', 1))# 用户不传页码默认为1
user_list = models.User.objects.all()# 获取所有数据'''Paginator 对象'''
# 实例化
paginator = Paginator(user_list, 10)# 分页的数据,每页显示十行
# print(paginator.count)# 总条数500
# print(paginator.num_pages)# 总页数 50
# print(paginator.per_page)# 每页显示条数 10
# print(paginator.page_range) # 生成器,next拿一页range(1,51)
# print(paginator.page(1))# page(1)第一页,()内写几可以拿到的页
# 判断页码数和11的关系,布局'''Page对象'''
try:
page = paginator.page(user_page_num)# 获取当前页
# print(page.has_next())# 判断有没有下一页True
# print(page.next_page_number())# 下一页页码数3
# print(page.has_previous())# 判断有没有上一页 True
# print(page.previous_page_number())# 上一页页码 1
# print(paginator.object_list)# 获取该页的所有数据的对象
except Exception as e:
user_page_num = paginator.num_pages# 如果没有搜索页设置默认数显示最后一页
page = paginator.page(user_page_num)# 没有搜索页显示最后一页
# print(page.number)# 获取当前页码 2if paginator.num_pages > 11:
# 开头的判断,如果当前页-5小于1,那么显示1,12
if user_page_num - 5 < 1:
page_range = range(1, 12)
# 末尾的判断,如果当前页+5大于总页码数,回退11
elif user_page_num + 5 > paginator.num_pages:
page_range = range(paginator.num_pages - 10, paginator.num_pages + 1)
# 中间当前页-5,+6
else:
page_range = range(user_page_num - 5, user_page_num + 6)
else:
page_range = paginator.page_range# 如果小于11那么就显示少有的对象
return render(request, 'user_page.html', locals())def delete(request):
del_id = request.GET.get('del_id')
# is_delete = 0
models.User.objects.filter(pk=del_id).delete()
# models.User.objects.filter(pk=del_id).update(is_delete=1)
import time
time.sleep(3)
return redirect('/user_list/')
Title - 锐客网 {#数据#}Uesr-Info
id
name
phone
address
action
{#获取当前页的所有对象#}
{% for user in page.object_list %}
{{ user.id }}
{{ user.name }}
{{ user.phone }}
{{ user.address }}
删除
{% endfor %}
{#分页#}
分页器模板
class Pagination(object):
def __init__(self, current_page, all_count, per_page_num=2, pager_count=5):
"""
封装分页相关数据
:param current_page: 当前页
:param all_count:数据库中的数据总条数
:param per_page_num: 每页显示的数据条数
:param pager_count:最多显示的页码个数
"""
try:
current_page = int(current_page)
except Exception as e:
current_page = 1if current_page < 1:
current_page = 1self.current_page = current_pageself.all_count = all_count
self.per_page_num = per_page_num# 总页码
all_pager, tmp = divmod(all_count, per_page_num)
if tmp:
all_pager += 1
self.all_pager = all_pagerself.pager_count = pager_count
self.pager_count_half = int((pager_count - 1) / 2)
@property
def start(self):
return (self.current_page - 1) * self.per_page_num@property
def end(self):
return self.current_page * self.per_page_numdef page_html(self):
# 如果总页码 < 11个:
if self.all_pager <= self.pager_count:
pager_start = 1
pager_end = self.all_pager + 1
# 总页码> 11
else:
# 当前页如果<=页面上最多显示11/2个页码
if self.current_page <= self.pager_count_half:
pager_start = 1
pager_end = self.pager_count + 1# 当前页大于5
else:
# 页码翻到最后
if (self.current_page + self.pager_count_half) > self.all_pager:
pager_end = self.all_pager + 1
pager_start = self.all_pager - self.pager_count + 1
else:
pager_start = self.current_page - self.pager_count_half
pager_end = self.current_page + self.pager_count_half + 1page_html_list = []
# 添加前面的nav和ul标签
page_html_list.append('''
''')
return ''.join(page_html_list)