关键点:分页的页码page
计算出·起止位置
理解分页的逻辑
page = int(request.GET.get('page', 1))
# 分页的页码
start = (page-1)*10
end = page*10
queryset = models.PrettyNum.objects.filter(**data_dict).order_by("-level")[start:end]
计算出一共有多少页:
data_dict = {}
search_data =request.GET.get('q', "")
# 根据关键字进行搜索,如果关键字存在,就进行搜索,返回搜索出的queryset
if search_data:
data_dict["mobile__contains"] = search_data
page = int(request.GET.get('page', 1))
# 分页的页码
page_size = 10
start = (page-1)*page_size
end = page*page_size
queryset = models.PrettyNum.objects.filter(**data_dict).order_by("-level")[start:end]
total_count = models.PrettyNum.objects.filter(**data_dict).order_by("-level").count()
total_page_count, div = divmod(total_count,page_size) #向下取整
if div:
total_page_count += 1
# 页码
page_str_list = []
for i in range(1,total_page_count+1):
# range 前取后不取,所以还要加一
ele = '<li><a href="?page={}">{}</a></li>'.format(i, i)
page_str_list.append(ele)
page_string = mark_safe("".join(page_str_list))
# 如果不存在,返回所有
return render(request, 'pretty_list.html', {"queryset": queryset, "search_data":search_data,"page_string": page_string})
分页组件的封装:
from django.utils.safestring import mark_safe
class Pagination(object):
def __init__(self, request, queryset, page_size=10, page_param="page", plus=5):
page = request.GET.get(page_param, 1)
if "".isdecimal(): # 10进制的数
page = int(page)
else:
page = 1
self.page = page
self.page_size = page_size # 每页显示数据条数
self.start = (page - 1) * page_size
self.end = page * page_size
self.page_queryset = queryset[self.start:self.end]
total_count = queryset.count()
total_page_count, div = divmod(total_count, page_size)
if div:
total_page_count += 1 # 总页码数量
self.total_page_count = total_page_count
self.plus = plus
def html(self):
# 计算出,显示当前的前五页,后五页
if self.total_page_count <= 2 * self.plus + 1:
# 数据库中数据比较少,没有达到11页
start_page = 1
end_page = self.total_page_count
else:
# 数据库中数据比较多,大于11页
# 当前页小于5时,极小值
if self.page <= self.plus:
start_page = 1
end_page = 2 * self.plus + 1
else:
# 当前页大于5
# 当前页 + plus 大于总页面
if self.page + self.plus > self.total_page_count:
start_page = self.total_page_count - 2 * self.plus
end_page = self.total_page_count
else:
start_page = self.page - self.plus
end_page = self.page + self.plus
# 页码
page_str_list = ['<li><a href="?page={}">首页</a></li>'.format(1)]
# 上一页
if self.page > 1:
prev = '<li><a href="?page={}">上一页</a></li>'.format(self.page - 1)
else:
prev = '<li><a href="?page={}">上一页</a></li>'.format(1)
page_str_list.append(prev)
# 生成页码
for i in range(start_page, end_page):
if i == self.page:
ele = '<li class="active"><a href="?page={}">{}</a></li>'.format(i, i)
else:
ele = '<li><a href="?page={}">{}</a></li>'.format(i, i)
page_str_list.append(ele)
# 下一页
if self.page < self.total_page_count:
prev = '<li><a href="?page={}">下一页</a></li>'.format(self.page + 1)
else:
prev = '<li><a href="?page={}">下一页</a></li>'.format(self.total_page_count)
page_str_list.append(prev)
# 尾页
page_str_list.append('<li><a href="?page={}">尾页</a></li>'.format(self.total_page_count))
search_string = """
<li>
<form style="float: left;margin-left: -1px" method="get">
<div class="input-group" style="width: 200px">
<input type="text" name="page" class="form-control" placeholder="页码">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">跳转</button>
</span>
</div>
</form>
</li>
"""
page_str_list.append(search_string)
page_string = mark_safe("".join(page_str_list))
return page_string