Home > Article > Backend Development > How to implement paging function in webpy_PHP tutorial
Paging is something you will definitely encounter when doing WEB development, but webpy does not have a built-in distribution class. You can only write a simple paging class by yourself. This article uses webpy (actually just a function of python) to implement a simple paging class...
In the past, paging and DB were often mixed together, such as the following PHP code:
<?php $page = get_current_page(); $start = $page*$step; $article_list = $db->all('select * from `xxx` limit $start,$step;'); $total = $db->get('select count(*) as `total` form `xxx`;'); //...pagination...
Such paging is obviously unscientific.
In order to meet the above three conditions, a simple paging class was designed (just an example, application to the project requires optimized code and strict inspection)
class ProbbsPage: def __init__(self, total, per = 10): self.total = total self.per = per self.url = '' self.page = 1 def set_url(self,url): self.url = url return self def set_page(self,page): self.page = int(page) return self def show(self): if self.total%self.per == 0: pages = self.total/self.per else: pages = self.total/self.per+1 if self.page < 6: limit_s = 1 else: limit_s = self.page if pages < (limit_s+10): limit_e = pages else: limit_e = limit_s+10 pagination = '<span>%s/%s pages </span>'%(self.page,pages) for i in range(limit_s,limit_e+1): if i == self.page: pagination += '<a class="cur" href="javascript:void(0);">%s</a>'%(i,) else: pagination += '<a href="%s">%s</a>'%(self.url%i,i) return pagination
pagination =ProbbsPage(总页数,每页数) url = 'your_page?page=%s'; page_html = pagination.set_url(url).set_page(page).show() print page_html #就可以显示出 #<span>当前页/共几页</span> #<a>页页链接</a>
set_url can be automatically extracted based on the url, provided that commonly used forms such as "?Page parameter=what page" are used (it is not possible if I use a special unexpected form)
set_page is the same as above, and can be automatically Extract from the url
If you do the above two steps, you can directly ProbbsPage (total number of pages, number of each page).show(), which is relatively convenient
Article source: http://pjiaxu.com/python/48.html