def query(request):
"""
current_page:当前页码,默认第一页
line_num:每页显示的行数,默认10行
table_line_count:总行数
page_count:总页数
display_page_num_count:显示的页数码标签总个数,默认10个
"""
current_page = int(request.GET.get('p',1))
line_num = int(request.COOKIES.get("per_page_line_num",10))
table_line_count=tablename.objects.count() ####统计表总行数,tablename指数据库中的表,根据实际修改
display_page_num_count=10
####求总页数
a,b=divmod(table_line_count,line_num) ####求除数和余数,例如divmod(5,1)=(1,1)
if b>0:
page_count=a+1
else:
page_count = a
"""
first_page:首页
last_page:尾页
prev_page:上一页
next_page:下一页
first_display_page_num:第一个显示的页码
last_display_page_num:最后一个显示的页码
"""
first_page = "%s" % (1, "首页")
last_page = "%s" % (page_count, "尾页")
if current_page-1==0:
prev_page="%s" % (current_page, "上一页")
else:
prev_page="%s" % (current_page-1, "上一页")
if current_page==page_count:
next_page="%s" % (current_page, "下一页")
else:
next_page="%s" % (current_page+1, "下一页")
page_str=""
page_str+=first_page
page_str+=prev_page
if page_count<=display_page_num_count:
first_display_page_num = 1
last_display_page_num = page_count + 1
else:
# 总页数大于10页(只显示10个页数标签)
# 如果当前页数《=5
# 显示1-10页数标签
# 如果当前页数x》5
# 显示 x-4到x+5页数标签
# 如果当前页数x》总页数-5
# 显示 总页数-9 到总页数之间的标签
if current_page_num<=5:
first_display_page_num=1
last_display_page_num=display_page_num_count
elif 5
first_display_page_num=current_page_num-5+1
last_display_page_num=current_page_num+5
else:
first_display_page_num=page_count-5-5+1
last_display_page_num=page_count
for i in range(first_display_page_num, last_display_page_num):
if i == current_page:
page_str += "%s" % (i, i)
else:
page_str += "%s" % (i, i)
page_str += next_page
page_str += last_page
page_str = mark_safe(page_str)
response = render(request, 'query.html', locals())
response.set_cookie("per_page_line_num",line_num,path='/query/') ####设置每页显示的行数
return response