我无法在 ndb 分页中进入“上一页”。
def show_feedback(kind, bookmark=None):
"""Renders returned feedback."""
cursor = None
more_p= None
if bookmark:
cursor = Cursor(urlsafe=bookmark)
q = Feedback.query()
q_forward = q.filter(Feedback.kind==Feedback.KINDS[kind]).order(-Feedback.pub_date)
q_reverse = q.filter(Feedback.kind==Feedback.KINDS[kind]).order(Feedback.pub_date)
feedbacks, next_cursor, more = q_forward.fetch_page(app.config['FEEDBACK_PER_PAGE'], start_cursor=cursor)
if cursor:
rev_cursor = cursor.reversed()
feedbacks2, prev_cursor, more_p = q_reverse.fetch_page(app.config['FEEDBACK_PER_PAGE'], start_cursor=rev_cursor)
next_bookmark = None
prev_bookmark = None
if more and next_cursor:
next_bookmark = next_cursor.urlsafe()
if more_p and prev_cursor:
prev_bookmark = prev_cursor.urlsafe()
return render_template_f11('show_feedback.html', kind=kind, feedbacks=feedbacks, next_bookmark=next_bookmark, prev_bookmark=prev_bookmark)
html:
{% if prev_bookmark %}
<a href="{{ url_for(request.endpoint, bookmark=prev_bookmark) }}">Previous</a>
{% endif %}
{% if next_bookmark %}
<a href="{{ url_for(request.endpoint, bookmark=next_bookmark) }}">Next</a>
{% endif %}
我可以正确翻页直到结束。但是我不能向后翻页直到最后一页,即使那样我也不能向后翻页直到第一页。
请问我错过了什么?
更新:
根据 Faisal 的建议更改了代码。我必须承认它的效果更好。但是分页仍然无法正常工作:
我有 7 个条目。配置中的 PAGE_SIZE 为 3。因此我们得到三个页面:
单击下一步时,我得到 7,6,5 -> 4,3,2 -> 1 Perfect。现在点击上一个时:1 -> 3,4,5 (?) -> 5,6,7 (?)
谢谢你的帮助
def show_feedback(kind, bookmark=None):
"""Renders returned feedback."""
is_prev = request.args.get('prev', False)
cursor = None
if bookmark:
cursor = Cursor(urlsafe=bookmark)
q = Feedback.query()
q_forward = q.filter(Feedback.kind==Feedback.KINDS[kind]).order(-Feedback.pub_date)
q_reverse = q.filter(Feedback.kind==Feedback.KINDS[kind]).order(Feedback.pub_date)
qry = q_reverse if is_prev else q_forward
feedbacks, cursor, more = qry.fetch_page(app.config['FEEDBACK_PER_PAGE'], start_cursor=cursor)
if is_prev:
prev_bookmark = cursor.reversed().urlsafe() if more else None
next_bookmark = bookmark
else:
prev_bookmark = bookmark
next_bookmark = cursor.urlsafe() if more else None
return render_template_f11('show_feedback.html', kind=kind, feedbacks=feedbacks, next_bookmark=next_bookmark, prev_bookmark=prev_bookmark)
更新 2:
现在看来它几乎可以使用 reverse()。
7,6,5 -> 下一个 -> 4,3,2 -> 下一个 -> 1
1 -> prev -> 2,3,4 -> 5,6,7 (订单不再是最晚日期优先)