首页 技术 正文
技术 2022年11月7日
0 收藏 386 点赞 411 浏览 2936 个字

如果查询出的数据,被分页了,这个时候翻页的时候应该讲页数的参数和查询的参数一块在URL上显示

"""
分页器
"""from django.utils.safestring import mark_safe
from django.http import QueryDictclass Pagination: # request 为request请求, all_count为所有数据的个数, query_params为查询的时候将查询结果与页数进行拼接, per_num为一页展示多少数据, max_show分多少页
def __init__(self, request, all_count, query_params=QueryDict(), per_num=10, max_show=11):
# 基本的URL
self.base_url = request.path_info
# 查询条件
self.query_params = query_params
self.query_params._mutable = True
# 当前页码
try:
self.current_page = int(request.GET.get('page', 1))
if self.current_page <= 0:
self.current_page = 1
except Exception as e:
self.current_page = 1
print(e)
# 最多显示的页码数
self.max_show = max_show
half_show = max_show // 2 # 每页显示的数据条数
self.per_num = per_num
# 总数据量
self.all_count = all_count # 总页码数
self.total_num, more = divmod(all_count, per_num)
if more:
self.total_num += 1 # 总页码数小于最大显示数:显示总页码数
if self.total_num <= max_show:
self.page_start = 1
self.page_end = self.total_num
else:
# 总页码数大于最大显示数:最多显示11个
if self.current_page <= half_show:
self.page_start = 1
self.page_end = max_show
elif self.current_page + half_show >= self.total_num:
self.page_end = self.total_num
self.page_start = self.total_num - max_show + 1
else:
self.page_start = self.current_page - half_show
self.page_end = self.current_page + half_show @property
def start(self):
return (self.current_page - 1) * self.per_num @property
def end(self):
return self.current_page * self.per_num @property
def show_li(self):
# 存放li标签的列表
html_list = [] self.query_params['page'] = 1
# query=a&page=1 first_li = '<li><a href="{}?{}" rel="external nofollow" rel="external nofollow" >首页</a></li>'.format(self.base_url, self.query_params.urlencode())
html_list.append(first_li) if self.current_page == 1:
prev_li = '<li class="disabled"><a><<</a></li>'
else:
self.query_params['page'] = self.current_page - 1
prev_li = '<li><a href="{0}?{1}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><<</a></li>'.format(self.base_url, self.query_params.urlencode())
html_list.append(prev_li) for num in range(self.page_start, self.page_end + 1):
self.query_params['page'] = num
if self.current_page == num:
li_html = '<li class="active"><a href="{0}?{1}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{2}</a></li>'.format(self.base_url,
self.query_params.urlencode(), num)
else:
li_html = '<li><a href="{0}?{1}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{2}</a></li>'.format(self.base_url,
self.query_params.urlencode(), num)
html_list.append(li_html) if self.current_page == self.total_num:
next_li = '<li class="disabled"><a>>></a></li>'
else:
self.query_params['page'] = self.current_page + 1
next_li = '<li><a href="{0}?{1}" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >>></a></li>'.format(self.base_url, self.query_params.urlencode()) html_list.append(next_li) self.query_params['page'] = self.total_num
last_li = '<li><a href="{}?{}" rel="external nofollow" rel="external nofollow" >尾页</a></li>'.format(self.base_url, self.query_params.urlencode())
html_list.append(last_li) return mark_safe(''.join(html_list))
# 解决搜索后的url翻页拼接问题
print('query', request.GET) # <QueryDict: {'query': [结果]}>
# query=结果
print(request.GET.urlencode())
# _mutable=True 这样就可以修改了
# query_params = copy.deepcopy(request.GET)
query_params = request.GET.copy() # 为了将查询结果与页数都放到url上
# query_params._mutable = True page = pagination.Pagination(request, customer.count(), query_params, per_num=3)
return render(request, 'crm/customer_list.html', {
"customer": customer[page.start:page.end],
'html_str': page.show_li,
})
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903