Google app engine python反向分页

Google app engine python反向分页,google-app-engine,app-engine-ndb,Google App Engine,App Engine Ndb,我已经尝试了基于GoogleAppEngine文档的示例进行反向分页 我的问题集中在这个例子上: # 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.

我已经尝试了基于GoogleAppEngine文档的示例进行反向分页

我的问题集中在这个例子上:

# 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):
#测试=测试(t=“a”);
#测试。put();
#测试2=测试(t=“b”);
#测试2.put();
#测试3=测试(t=“c”);
#测试3.put();
#测试4=测试(t=“d”);
#测试4.put();
#测试5=测试(t=“e”);
#测试5.put();
cursor=ndb.cursor.from\u websafe\u字符串(self.request.get(“c”))
如果光标:
q=Testing.query()
q_forward=q.order(Testing.key)
q_反向=q顺序(-Testing.key)
条,下一个光标,更多=前进。获取页面(2,开始光标=光标)
rev_cursor=cursor.reversed()
bars1,上一个光标,more1=q\u反向。获取页面(2,开始光标=rev\u光标)
self.response.write(“
”) 其他: q=Testing.query() q_forward=q.order(Testing.key) q_反向=q顺序(-Testing.key) 条,下一个光标,更多=前进。获取页面(2) self.response.write('helloworld!
')) 对于酒吧中的酒吧: self.response.write(bar.t+“
”) self.response.write(“”)
但我还是不明白为什么,它不能完美地回到上一页。。。 当我单击“将第1页转发到第2页”时:

第1页:a、b 第2页:c、d

但当我向后点击时:

第2页:c、d 第1页:b、c(应为:a、b)


这让我很困惑,因为论坛上的某个人可以根据这个示例使它工作,而没有人给出他们的示例代码…

问题是您使用
c
来指代前后光标

因此,当您得到一个已经反转的向后光标时,您正在调用

rev_cursor = cursor.reversed()
以前

bars1, prev_cursor, more1 = q_reverse.fetch_page(2, start_cursor=rev_cursor)
因此,在开始查询之前,光标开始指向另一个方向

要查看更简单的测试,请定义相同的模型并预填充一些数据:

from google.appengine.ext import ndb
class Testing(ndb.Model):
  t = ndb.StringProperty()
ndb.put_multi([Testing(t='a'), Testing(t='b'), Testing(t='c')])
通过查询前两个元素获取光标:

>>> q_forward = Testing.query().order(Testing.t)
>>> result, forward_cursor, _ = q_forward.fetch_page(2)
>>> print [value.t for value in result]
[u'a', u'b']
反向查询中使用该光标,但不要将其反向

>>> reverse_cursor = forward_cursor.reversed()
>>> result, _, _ = q_reverse.fetch_page(2, start_cursor=reverse_cursor)
>>> print [value.t for value in result]
[u'b', u'a']

与执行此操作后的反向查询相比:

我看不出您在何处定义了
q\u反向
查询以进行更简单的测试。我很感兴趣,谢谢。啊,没关系,只是
q\u reverse=Testing.query().order(-Testing.t)
。实际上,我想向后翻页,但仍然看到顺序是
['a','b']
>>> reverse_cursor = forward_cursor.reversed()
>>> result, _, _ = q_reverse.fetch_page(2, start_cursor=reverse_cursor)
>>> print [value.t for value in result]
[u'b', u'a']