用 paginate 替换 webhelpers:如何移植 PageURL_WebOb? (py2 到 py3)

Replacing webhelpers with paginate: how to port PageURL_WebOb? (py2 to py3)

移植 Python 2 到 Python 3

如您所见,here Python 2 包 webhelpers.paginate 在 Python 3 下不再存在。 相反,他们为 Python 3 创建了一个额外的模块 paginate(可以在 here 中找到)。

Python 2

在 Python 2 下使用 pyramid 时,代码如下所示:

# request is a pyramid request
def get_paginator(request, page=1, items_per_page=10):
    page_url = PageURL_WebOb(request)
    return Page(sql-query-here, page, url=page_url, items_per_page=items_per_page)

Python 3

在 Python 3 下,他们从 paginate 中删除了 PageURL_WebOb。 那么是否有可能直接从 request 检索 page_url

Python 3 的正确端口是什么样的?

因此,在 Steve Piercythis issue 官方 paginate git 的帮助下,我能够将其移植到以下位置方式:

# request is a pyramid request
def get_paginator(request, page=1, items_per_page=10):
    def url_maker(page_number):
        query = request.GET
        query["page"] = str(page_number)
        return request.current_route_url(_query=query)

    return Page(sql-query-here, page=page, items_per_page=items_per_page, url_maker=url_maker)