按照老师的建议做了下优化,在index.py中page_params增加了'page_show_num': 10,设置页码的展示个数,此处为展示10个页码:
page_params = {
'total_count': query.count(),
'page_size': 30,
'page_show_num': 10,
'page': page,
'url': "/?"
}
然后在Hepler.py增加了如下代码:
page_show_num = int(params['page_show_num'])
half_show_num = math.floor((page_show_num +1) / 2)
# 计算页码展示的数量
start_num = 1
end_num = total_pages
if page_show_num < total_pages:
if page > half_show_num:
start_num = page - (half_show_num-1) if page + (half_show_num-1) < total_pages else total_pages - (half_show_num-1)*2
end_num = page + (half_show_num-1) if page + (half_show_num-1) < total_pages else total_pages
else:
end_num = page_show_num
range_page = range(start_num, end_num + 1)
is_prev = 0 if page <= 1 else 1
is_next = 0 if page >= total_pages else 1
pages = {
'current': page,
'total_pages': total_pages,
'total': total_count,
'page_size': page_size,
'is_next': is_next,
'is_prev': is_prev,
'range': range_page,
'url': params['url']
}