分页一:
page_num = request.GET.get('page', default='1')
try:
page = paginator.page(page_num)
print("页面数据为:", page)
except PageNotAnInteger:
page = paginator.page(1)
print('输入页数非整数', paginator.num_pages)
except EmptyPage:
if int(page_num) > paginator.num_pages:
page = paginator.page(paginator.num_pages)
elif int(page_num) < paginator.num_pages:
page = paginator.page(1)
page_num = int(page_num)
# 前端页码显示设置
if page_num <= 3:
if paginator.num_pages <= 5:
dis_range = range(1, paginator.num_pages + 1)
else:
dis_range = range(1, 5 + 1)
else:
if (page_num + 2) >= paginator.num_pages + 1: # 后面加两个就超范围了要重新算
dis_range = range(page_num - (5 - (paginator.num_pages - page_num + 1)), paginator.num_pages + 1)
else:
dis_range = range(page_num - 2, page_num + 2 + 1) # 前后都没超范围直接前减2后加2保持当前页在中间
return render(request, 'first.html',
{'paginator': paginator, 'page_size': page_size, 'page': page, 'dis_range': dis_range,
'search_Key': search_Key, 'search_Text': search_Text})
分页二:
# 分页
p = Paginator(newList, 5)
if p.num_pages <= 1:
pageData = ''
else:
page = int(request.GET.get('page', 1))
newList = p.page(page)
left = []
right = []
left_has_more = False
right_has_more = False
first = False
last = False
total_pages = p.num_pages
page_range = p.page_range
if page == 1:
right = page_range[page:page + 2]
print(total_pages)
if right[-1] < total_pages - 1:
right_has_more = True
if right[-1] < total_pages:
last = True
elif page == total_pages:
left = page_range[(page - 3) if (page - 3) > 0 else 0:page - 1]
if left[0] > 2:
left_has_more = True
if left[0] > 1:
first = True
else:
left = page_range[(page - 3) if (page - 3) > 0 else 0:page - 1]
right = page_range[page:page + 2]
if left[0] > 2:
left_has_more = True
if left[0] > 1:
first = True
if right[-1] < total_pages - 1:
right_has_more = True
if right[-1] < total_pages:
last = True
pageData = {
'left': left,
'right': right,
'left_has_more': left_has_more,
'right_has_more': right_has_more,
'first': first,
'last': last,
'total_pages': total_pages,
'page': page,
}
return render(
request, 'newList.html', {
'active_menu': 'news',
'sub_menu': submenu,
'newName': newName,
'newList': newList,
'pageData': pageData,
})