如何使用 webhelpers.paginate 在分页期间保留查询参数
我从 http://rapidprototype.ch/bg2docs/tg2pagination.html 查看分页示例对于我的 Turbogears 2 项目,它工作得很好,但是,当我更改我正在查找的页面时,我的查询参数出现了问题。
这是我列出时控制器中的内容。
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))
这是 html 模板片段
<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>
搜索工作正常,产生像这样的链接“/list?q=cable”,但是当我单击某些分页页面“1,2...8,9”时变成 '/list?page=2'
如何将以前的查询参数或任何其他参数添加到链接?
I look an example of paginating from http://rapidprototype.ch/bg2docs/tg2pagination.html for my Turbogears 2 project and it works great but, I have a problem regarding my query parameters when I change the page I'm looking.
This is what I have in my controller when listing.
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 the html template fragment
<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 searches works fine resulting in links like this '/list?q=cable' but when I click some of the paginating pages "1,2...8,9" turns to '/list?page=2'
How do I add my previous query parameter or any other parameters to the link?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 shell 上试验了一段时间后,我想我找到了解决方案。
currentPage 中定义了一个 kwargs 字典(从 paginate.Page 分配后),所以我做了一些发送参数的实验,它起作用了。就是这样。
现在我得到了这种链接:“/list?q=cable&page=2”仍然想知道这是否是最佳解决方案或最佳实践
After experimenting on the shell for a while I think I found a solution.
There's a kwargs dictionary defined in currentPage (after being assigned from paginate.Page), so I made some experiments sending parameters and it worked. This is how.
now I get this kind of links: '/list?q=cable&page=2' still wondering if it is the best solution or the best practice
你应该使用如下语法:
you should use syntax like:
您可以更新请求参数,就像这个片段一样。
you can update request params, like this snippets.