python - Replacing webhelpers with paginate: how to port PageURL_WebOb? (py2 to py3)
问题描述
Porting Python 2 to Python 3
As you can read here the Python 2 package webhelpers.paginate
no longer exists under Python 3.
Instead they created an extra module paginate
for Python 3 (which can be found here).
Python 2
When working with pyramid
under Python 2, the code looks like this:
# 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
Under Python 3 they removed the PageURL_WebOb
from paginate
.
So is it somehow possible to retrieve the page_url
directly from the request
?
How would a correct port to Python 3 look like?
解决方案
So with the help of Steve Piercy and this issue from the official paginate
git I was able to port it in the following way:
# 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)
推荐阅读
- android - 在 SQLite 数据库的单元测试中使用上下文
- jquery - 错误未捕获类型错误:无法读取完整日历上未定义的属性“hasTime”
- html - 使用 Angular 6 在输入中单击后如何更改框阴影?
- java - 一个java纸牌游戏程序的dealHands、dealCard和displayHands方法问题
- node.js - 模块热更换被禁用
- android - 如何在房间数据库中运行触发器?
- c - 为什么我在这个 C 代码中得到错误的答案?
- java - 在不知道其架构或域对象类型的情况下解析 JSON
- node.js - 如何将一个猫鼬集合的 ID 添加到另一个集合中的数组中?
- spring-mvc - 为什么我的 preHandle 拦截器在控制器方法之后调用