-
Notifications
You must be signed in to change notification settings - Fork 19
Pagination
The Whirlwind pagination setup is pretty straight forward. It's all centered around the whirlwind.view.Paginator class. The Paginator object takes a mongo collection and provides you with convenient paging functions that you would expect and have prob seen in other frameworks.
page() - gets the current page of documents
page_count() - get the total number of pages available
has_previous() & has_next() - this tells you if previous or next pages are available. helpful when building out your paging controls
previous_page() & next_page() - gets the page number for the previous and next pages. helpful when generating paging bac k& next links
previous_page_link() & next_page_link() - builds you a link to the previous and next pages
You can also use the core.request.RequestHelper.paged_list function to help you build out your pagination. Please note that since this function uses the MongoDB skip functionality there is a performance hit when paging large collections. Alternatively you can also use the core.request.RequestHelper.sliced_list function for bigger lists. This method uses max and min Document ids to page more efficiently. There are some drawbacks to this method as well. Currently the Paginator object does not work with the sliced_list function. Also due to the way the query is structured the sliced_list function only supports sorting by the _id value.
In your RequestHandler
@route('/beers')
class ListBeersHandler(BaseRequest):
def get(self):
#get the current page param
page = handler.get_argument('page',1)
page = page if page >= 1 else 1
#get the document count param
count = handler.get_argument('count',10)
count = count if count >= 1 else 10
#find all your beers (buuuurp)
beers = Mongo.db.ui.beers.find()
#create a Paginator object
paginator = Paginator(beers,page,count,beers.count())
#pass the paginator object to the template
template_values = {
'paginator':paginator
}
self.render_template('/beers/list.html',**template_values)
In your template
% for beer in paginator.page()
${beer.name}<br />
% endfor
<!-- if there is a previous page print a back link -->
% if paginator.has_previous():
<a href="${paginator.previous_page_link()}"><< back</a>
% endif
<!-- if there is a previous and a next page print a divider -->
% if paginator.has_previous() and paginator.has_next():
|
% endif
<!-- if there is a next page print a next link -->
% if paginator.has_next():
<a href="${paginator.next_page_link()}">next >></a>
% endif