How to preserve query parameters during pagination using webhelpers.paginate
I am looking at a pagination example from http://rapidprototype.ch/bg2docs/tg2pagination.html for my Turbogears 2 project and it works great, but I have a problem with my request parameters when I change the page that I am looking for.
This is what I have in my controller when enumerating.
def list(self, page=1, **kw):
q = ""
if kw.has_key('q'):
log.debug("searching %s" % kw)
q = kw['q']
if kw.has_key('all'):
q = ""
products = DBSession.query(model.Product).filter(
or_(model.Product.name.like('%%%s%%' % q),
model.Product.description.like('%%%s%%' % q),
model.Product.model.like('%%%s%%' % q),
model.Product.code.like('%%%s%%' % q))).all()
def get_link(product):
return Markup("""<a href="form?id=%s">%s</a>""" % (product.id, product.id))
product_fields = [
(Markup("""<a href="?s=id">Id</a>"""), get_link),
(u'Name', 'name'),
(u'Model', 'model'),
(u'Code', 'code'),
(u'Description', 'description')]
product_grid = MyDataGrid(fields = product_fields)
currentPage = paginate.Page(products, page, items_per_page=50)
return dict(currentPage=currentPage,
title=u'Products List', item=u'product', items=u'products',
data=currentPage.items,
grid=product_grid,
page=u'Search %s results' % q,
q=q,
hits=len(products))
This is a snippet of the html template
<h1>List of ${items}</h1>
<form action="list" method="get">
<input name="q" type="text" value="${value_of('q', default='')}"/>
<input type="submit" value="Search"/> <input type="submit" name="all" value="All"/>
</form>
${hits} ${items} found
<p class="pagelist">${currentPage.pager(format='$link_first ~3~ $link_last')}</p>
<div>
${grid(data)}
</div>
<p><a href="${tg.url('form')}">Add a ${item}</a></p>
The search works fine and results in links like this "/ list? Q = cable", but when I click on some of the paginated pages "1,2 ... 8,9" you turn to "/ list ? page = 2 "
How do I add my previous query parameter or any other parameters to the link?
source to share
After experimenting with the shell for a while, I think I have found a solution.
There's a kwargs dictionary defined in currentPage (after assigning from paginate.Page), so I did some experiments sending parameters and it worked. Here's how.
currentPage = paginate.Page(products, page, items_per_page=50)
currentPage.kwargs['q'] = q
return dict(currentPage=currentPage,
title=u'Products List', item=u'product', items=u'products',
data=currentPage.items,
grid=product_grid,
page=u'Search %s results' % q,
q=q,
hits=len(products))
now I get links like this: '/ list? q = cable & page = 2 'still wondering if this is the best solution or best practice
source to share
you can update query parameters like these snippets.
def paginate(self, items, items_per_page=20):
"""https://bitbucket.org/bbangert/webhelpers/src/acfb17881c1c/webhelpers/paginate.py"""
current_page = self.request.GET.get('page') or 1
def page_url(page):
params = self.request.params.copy()
params['page'] = page
return self.request.current_route_url(_query=params)
return Page(collection=items, page=current_page, items_per_page=items_per_page, url=page_url)
source to share