分页

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
from django.utils.safestring import mark_safe


def Pager(page_count, current_page):
html_dict = []
first_page = '<a href="/hosts/1">首页</a>'
html_dict.append(first_page)
if current_page - 1 == 0:
prev_page = '<a href="#">上一页</a>'
else:
prev_page = '<a href="/hosts/%s">上一页</a>' % (current_page - 1)
html_dict.append(prev_page)
if page_count < 12:
start = 0
end = page_count
elif current_page - 6 <= 0:
start = 0
end = 11
elif current_page + 5 >= page_count:
start = page_count - 11
end = page_count
else:
start = current_page - 6
end = current_page + 5
for i in range(start, end):
if i + 1 == current_page:
html = '<a href="/hosts/%s" class="selected">%s</a>' % ((i + 1), (i + 1))
else:
html = '<a href="/hosts/%s">%s</a>' % ((i + 1), (i + 1))
html_dict.append(html)
if current_page == page_count:
last_page = '<a href="#">下一页</a>'
else:
last_page = '<a href="/hosts/%s">下一页</a>' % (current_page + 1)
html_dict.append(last_page)

end_page = '<a href="/hosts/%s">尾页</a>' % (page_count)
html_dict.append(end_page)
return mark_safe(''.join(html_dict))

原文地址:https://www.cnblogs.com/3one/p/5846182.html