如何使用 webhelpers.paginate 在分页期间保留查询参数

发布于 2024-08-07 03:34:35 字数 2104 浏览 7 评论 0原文

我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黑色毁心梦 2024-08-14 03:34:35

在 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”仍然想知道这是否是最佳解决方案或最佳实践

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.

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 this kind of links: '/list?q=cable&page=2' still wondering if it is the best solution or the best practice

真心难拥有 2024-08-14 03:34:35

你应该使用如下语法:

currentPage.kwargs['q'] = q

currentPage = paginate.Page(
                            products,
                            page,
                            items_per_page=50,
                            q = q
)

you should use syntax like:

currentPage.kwargs['q'] = q

currentPage = paginate.Page(
                            products,
                            page,
                            items_per_page=50,
                            q = q
)
薄荷梦 2024-08-14 03:34:35

您可以更新请求参数,就像这个片段一样。

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)

you can update request params, like this 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文