"""自定义分页组件"""
class Pagination(object):
def __init__(self, request, queryset, page_param="page", page_size=10, plus=5):
"""
:param request: 请求对象
:param queryset: 符合条件的数据以queryset的方式传入
:param page_param: 在url中传递的参数:/manager/user/list/?query=1
:param page_size: 每页多少条数据
:param plus: 分页组件页码显示,默认显示前五个和后五个页码
"""
page = request.GET.get(page_param, "1")
from django.http.request import QueryDict
import copy
query_dict = copy.deepcopy(request.GET)
query_dict._mutable = True
self.page_queryset_dict = query_dict
if page.isdecimal():
page = int(page)
else:
page = 1
self.page = page
self.page_param = page_param
self.page_size = page_size
self.start = (page - 1) * page_size
self.end = page * page_size
self.page_queryset = queryset[self.start:self.end]
self.total_count = queryset.count()
total_page_count, div = divmod(self.total_count, page_size)
if div:
total_page_count += 1
self.total_page_count = total_page_count
self.plus = plus
def createhtml(self):
page_list = []
from django.utils.safestring import mark_safe
total_page_count, div = divmod(self.total_count, self.page_size)
self.page_queryset_dict.setlist(self.page_param, [1])
first_page = '<li><a href="?{}" aria-label="Previous"><span aria-hidden="true">首页</span></a></li>'.format(
self.page_queryset_dict.urlencode())
page_list.append(first_page)
if div:
total_page_count += 1
if total_page_count <= 2 * self.plus + 1: # 数据不足11页
start_page = 1
end_page = total_page_count
elif self.page <= self.plus: # 最小值
start_page = 1
end_page = 2 * self.plus + 1
elif self.page + self.plus > total_page_count: # 最大值
start_page = total_page_count - 2 * self.plus
end_page = total_page_count + 1
else:
start_page = self.page - self.plus
end_page = self.page + self.plus + 1
self.page_queryset_dict.setlist(self.page_param, [self.page - 1])
if self.page > 1:
prev = '<li><a href="?{}" aria-label="Previous"><span aria-hidden="true">«</span></a></li>'.format(
self.page_queryset_dict.urlencode())
page_list.append(prev)
for i in range(start_page, end_page):
self.page_queryset_dict.setlist(self.page_param, [i])
if i == self.page:
ele = '<li class="active"><a href="?{}">{}</a></li>'.format(self.page_queryset_dict.urlencode(), i)
else:
ele = '<li><a href="?{}">{}</a></li>'.format(self.page_queryset_dict.urlencode(), i)
page_list.append(ele)
if self.page < total_page_count:
self.page_queryset_dict.setlist(self.page_param, [self.page + 1])
nextp = '<li><a href="?{}" aria-label="Previous"><span aria-hidden="true">»</span></a></li>'.format(
self.page_queryset_dict.urlencode())
page_list.append(nextp)
self.page_queryset_dict.setlist(self.page_param, [total_page_count])
last_page = '<li><a href="?{}" aria-label="Previous"><span aria-hidden="true">尾页</span></a></li>'.format(
self.page_queryset_dict.urlencode())
page_list.append(last_page)
search_str = """<li>
<form method="get" name="page" style="float: left;margin-left: -1px">
<div class="input-group">
<input type="text" name="page" class="form-control" placeholder="页码"
value="{}"
style="position: relative;float: left;display:inline-block;width: 88px">
<button class="btn btn-default" type="submit" style=>跳转</button>
</div>
</form>
</li>""".format(self.page)
page_list.append(search_str)
page_string = mark_safe("".join(page_list))
return page_string
分页组件(django可用,好用,适用性广)
于 2023-07-08 15:01:03 首次发布