分享一个Python数字分页工具

有点造轮子的嫌疑,感觉Django自带的分页工具不是很好用,部分重写了下

Python代码

class Pagination():
				'''
				Pagination tools
				'''
				count = 0
				page_size = 10
				current_page = 1
				page_gap = 5
				page_count = 0

				def __init__(self,object_list,page_size,current_page = 1,page_gap=5):
				u'''Simple Pagination tools
				object_list: The queue or list objects
				page_size:    Page size
				current_page: Current Page
				page_gap:    The gap of current page, inlcude left gap and right gap. the same value is this.
				'''
				self.object_list = object_list
				self.count = self._get_count()
				self.page_size = page_size
				self.current_page = current_page
				self.page_gap = page_gap
				if not self.count % page_size:
				self.page_count = self.count / page_size
				else:
				self.page_count = self.count / page_size + 1

				def _get_count(self):
				"Returns the total number of objects, across all pages."
				try:
				self.count = self.object_list.count()
				except (AttributeError, TypeError):
				self.count = len(self.object_list)
				return self.count

				def page_objects(self):
				bottom = (self.current_page-1)*self.page_size
				top = bottom + self.page_size
				if top >= self.count:
				top = self.count
				return self.object_list[bottom:top]

				def has_pre(self):
				if self.current_page > 1:
				return True
				return False

				def has_next(self):
				if self.current_page < self.page_count:
				return True
				return False

				def has_first(self):
				if self.current_page == 1:
				return False
				return True

				def has_last(self):
				if self.current_page == self.page_count:
				return False
				return True

				def page_limit(self):
				return self.page_size * (self.current_page - 1)

				def page_left_list(self):
				if self.current_page > self.page_gap:
				return range(self.page_count+1)[self.current_page-self.page_gap:self.current_page]
				return range(self.page_count+1)[1:self.current_page]

				def page_right_list(self):
				if self.current_page + self.page_gap <= self.page_count:
				return range(self.page_count+1)[self.current_page+1:self.current_page+self.page_gap+1]
				return range(self.page_count+1)[self.current_page+1:]

				def page_list(self):
				return range(self.page_count+1)[1:]

				def page_html_format(self):
				html = '<div id="pagination">'
				if self.has_first():
				html += '<a href="?page=1">First</a>n'
				html += ''.join(['<a href="?page=%s">%s</a>n'%(n,n) for n in self.page_left_list()])
				html += '<a class="a1">%s</a>n'%self.current_page
				html += ''.join(['<a href="?page=%s">%s</a>n'%(n,n) for n in self.page_right_list()])
				if self.has_last():
				html += '<a href="?page=%s">Last</a>n'%self.page_count
				html += '</div>'
				return html

使用方法很简单

...
				PAGE_SIZE=10
				try:
				pagination = Pagination(results,PAGE_SIZE,current_page=int(request.GET.get('page', 1)),page_gap=10)
				except InvalidPage:
				raise Http404

				context = {
				'query': sTerm,
				'object_list': pagination.page_objects(),
				'pagination': pagination,
				}
				return render_to_response('search/search.html',RequestContext(request,context))
				...

模板使用

{{ pagination.page_html_format }}

效果如下(自己定义CSS)

评论关闭。