【问题标题】:How to keep query parameters during pagination with webhelpers.paginate如何使用 webhelpers.paginate 在分页期间保留查询参数
【发布时间】:2009-10-05 02:45:19
【问题描述】:

我为我的 Turbogears 2 项目查看了一个从 http://rapidprototype.ch/bg2docs/tg2pagination.html 分页的示例,它运行良好,但是当我更改正在查看的页面时,我的查询参数出现问题。

这是我列出时控制器中的内容。

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'

如何将我之前的查询参数或任何其他参数添加到链接中?

【问题讨论】:

    标签: python pagination pylons genshi turbogears2


    【解决方案1】:

    在shell上试验了一段时间后,我想我找到了解决办法。

    在 currentPage 中定义了一个 kwargs 字典(在从 paginate.Page 分配之后),所以我做了一些发送参数的实验并且它起作用了。就是这样。

    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))
    

    现在我得到了这种链接:'/list?q=cable&page=2' 仍然想知道它是最佳解决方案还是最佳实践

    【讨论】:

    • 这已经被证明是有效的,还没有找到任何关于这个或任何其他方法的文档,我会把它标记为正确的答案。
    【解决方案2】:

    你应该使用如下语法:

    currentPage.kwargs['q'] = q
    
    currentPage = paginate.Page(
                                products,
                                page,
                                items_per_page=50,
                                q = q
    )
    

    【讨论】:

      【解决方案3】:

      您可以更新请求参数,例如 sn-ps。

      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)
      

      【讨论】:

        猜你喜欢
        • 2012-03-28
        • 1970-01-01
        • 1970-01-01
        • 2010-11-28
        • 2021-03-31
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        • 2017-09-22
        相关资源
        最近更新 更多