我试图根据 google appengine 文档的示例进行向后分页
我的问题集中在这个例子上:
# Set up.
q = Bar.query()
q_forward = q.order(Bar.key)
q_reverse = q.order(-Bar.key)
# Fetch a page going forward.
bars, cursor, more = q_forward.fetch_page(10)
# Fetch the same page going backward.
rev_cursor = cursor.reversed()
bars1, cursor1, more1 = q_reverse.fetch_page(10, start_cursor=rev_cursor)
基于这个例子,我创建了我自己的版本(像这样):
def get(self):
#testing = Testing(t="a");
#testing.put();
#testing2 = Testing(t="b");
#testing2.put();
#testing3 = Testing(t="c");
#testing3.put();
#testing4 = Testing(t="d");
#testing4.put();
#testing5 = Testing(t="e");
#testing5.put();
cursor = ndb.Cursor.from_websafe_string(self.request.get("c"))
if cursor:
q = Testing.query()
q_forward = q.order(Testing.key)
q_reverse = q.order(-Testing.key)
bars, next_cursor, more = q_forward.fetch_page(2, start_cursor=cursor)
rev_cursor = cursor.reversed()
bars1, prev_cursor, more1 = q_reverse.fetch_page(2, start_cursor=rev_cursor)
self.response.write("<a href=\"?c="+prev_cursor.to_websafe_string()+"\">«</a><br />")
else:
q = Testing.query()
q_forward = q.order(Testing.key)
q_reverse = q.order(-Testing.key)
bars, next_cursor, more = q_forward.fetch_page(2)
self.response.write('Hello world!<br />')
for bar in bars:
self.response.write(bar.t + "<br />")
self.response.write("<a href=\"?c="+next_cursor.to_websafe_string()+"\">»</a>")
但我仍然不明白为什么,它不能完美地返回上一页......当我单击将第 1 页转发到第 2 页时:
第 1 页:a、b 第 2 页:c、d
但是当我向后点击时:
第 2 页:c、d 第 1 页:b、c(应为:a、b)
这让我很困惑,因为论坛上的某个人可以根据这个例子使它工作,没有人给出他们的示例代码......