Pony ORM 的分页有什么最佳实践吗?
我看到其他人有这些 has_next 和 has_previous 辅助方法,但在 Pony 中我只能靠自己。
到目前为止,这就是我所拥有的,几个 Jinja helper :
# Jinja helpers for pagination
def next_page(current, max_page):
if current >= max_page:
return False
else:
return current + 1
def prev_page(current, max_page):
if current < 2:
return False
else:
return current - 1
max_page 的计算方式如下:math.ceil(MyTable.select().count()/PAGE_SIZE)
但是有点乏味,你必须不停地发送当前页面和最大页面。
{% if maxpage > 1 %}
{% if prev_page(page, maxpage) %}
<a href="{{ url_for('index', pagenum=prev_page(page, maxpage)) }}"><</a>
{% endif %}
{% if next_page(page, maxpage) %}
<a href="{{ url_for('index', pagenum=next_page(page, maxpage)) }}">></a>
{% endif %}
{% endif %}
所以我错过了什么吗?有什么更好的方法吗?
请您参考如下方法:
可以查询一个分页
q = models.select(d for d in models.Thingy)
page = 1
count = q.count()
results_of_first_page = q.page(page, 20)
pages = int(count/20)
if pages > 20:
pages = 20
