1、调用方式:
#views.py
total = 300 # 总记录数,一般是一个查询的总数 Books.objects.all().count()
import Page
pager = Page()
page = 3 # 第几页,一般是通过 GET 方式读取
pager.url = '/admin/list/page_num/' # 必选,其中的 page_num 会被替换成具体页码
pager.page_size = 16 # 可选,默认为一页16条
pager.places = 11 # 可选,默认为最多显示11个页码
page_str = pager.create_page(total, page)
2、templates
<div id="pages">
<p>{{ page_str | safe }} </p>
</div>
3、css
分页的scss代码
#pages {
margin-top: 10px;
float: left;
}
分页的scss代码
#pages a, 分页的scss代码
#pages span {
padding: 3px 8px;
margin-left: 2px;
margin-right: 2px;
border: 1px solid #ccc;
}
分页的scss代码
#pages a:hover, 分页的scss代码
#pages a:visited, 分页的scss代码
#pages a {
text-decoration: none;
color: blue;
}
分页的scss代码
#pages a.active {
color: #ccc;
font-weight: bold;
}
4、Page类
# 负责显示分页的类
class Page():
# 设置默认变量
def __init__(self):
self.page_size = 16 # 每页显示的数据量
self.places = 11 # 一共显示的页码个数
self.url = '' # 当前页面的 URL,里面的页面部分用 page_num 代替,如:/myadmin/image_list/page_num/
self.last_text = '上一页'
self.next_text = '下一页'
def create_page(self, count, page):
# 设置页码
page = self.count_page_edge(page, count, self.page_size)
offset = self.count_offset(page, self.page_size)
limit_left = offset[0]
limit_right = offset[1]
page_str = self.create_page_str(page, count, self.page_size, self.places)
page_str = '共 ' + str(count) + ' 条 ' + page_str
return page_str, limit_left, limit_right
# page 当前页码
# total 总数据量
# page_size 每页数据量
# places 一共显示的页码个数
# last_text 上一页文字
# next_text 下一页文字
def create_page_str(self, page, total, page_size, places, last_text='上一页', next_text='下一页'):
# 计算出总页数
page_count = total // page_size
# 如果总页数小于显示的页码个数,则显示的页码个数设置为总页数
if page_count < places:
places = page_count
# 如果有余数,则页数加1
if total % page_size > 0:
page_count = page_count + 1
if page < 1:
page = 1
# 如果当前页码大于总页数,则当前页码等于总页数
if page > page_count:
page = page_count
half = places // 2
# 显示首页
if page == 1:
the_first_page = ''
else:
the_first_page = '<a class="page-a" href="' + self.url.replace('page_num', '1') + '">首页</a>'
# 显示末页
if page == page_count:
the_last_page = ''
else:
the_last_page = '<a class="page-a" href="' + self.url.replace('page_num', str(page_count)) + '">末页</a>'
# 显示上一页
if page > 1:
last_page = '<a class="page-a" href="' + self.url.replace('page_num', str(page - 1)) + '">' + last_text + '</a>'
else:
last_page = ''
# 显示下一页
if page < page_count:
next_page = '<a class="page-a" href="' + self.url.replace('page_num', str(page + 1)) + '">' + next_text + '</a>'
else:
next_page = ''
page_num_str = ''
# 生成当前页前面的页码
if page > half:
if page > page_count - half:
begin_page = page_count - places + 1
else:
begin_page = page - half
else:
begin_page = 1
for i in range(begin_page, page):
page_num_str += '<a class="page-a" href="' + self.url.replace('page_num', str(i)) + '">' + str(i) + '</a>'
page_num_str += '<a class="active" href="#">' + str(page) + '</a>'
# 生成当前页后面的页码
if (page_count - page) >= half:
end_page = page + half
else:
end_page = page_count
if end_page <= places:
end_page = places
for i in range(page + 1, end_page + 1):
page_num_str += '<a class="page-a" href="' + self.url.replace('page_num', str(i)) + '">' + str(i) + '</a>'
page_str = the_first_page + last_page + page_num_str + next_page + the_last_page
return page_str
# 修正第一页和最后一页的页码
def count_page_edge(self, page, count, page_size):
page_count = count // page_size
if count % page_size > 0:
page_count = page_count + 1
if page < 1:
page = 1
if page > page_count:
page = page_count
return page
# 计算数据在表中的偏移量
def count_offset(self, page, page_size):
limit_left = (page-1) * page_size
limit_right = page * page_size
if limit_left < 0:
limit_left = 0
if limit_right < 0:
limit_right = 0
return (limit_left, limit_right)