python向后分页 [英] python backward paging

查看:24
本文介绍了python向后分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试根据 google appengine 文档示例进行反向分页

NDB 查询光标

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

# 设置.q = Bar.query()q_forward = q.order(Bar.key)q_reverse = q.order(-Bar.key)# 向前获取页面.条形,游标,更多 = q_forward.fetch_page(10)# 向后获取相同的页面.rev_cursor = cursor.reversed()bar1, cursor1, more1 = q_reverse.fetch_page(10, start_cursor=rev_cursor)

基于这个例子,我创建了我自己的版本(像这样):

def get(self):#testing = Testing(t="a");#testing.put();#testing2 = 测试(t="b");#testing2.put();#testing3 = 测试(t="c");#testing3.put();#testing4 = 测试(t="d");#testing4.put();#testing5 = 测试(t="e");#testing5.put();游标 = ndb.Cursor.from_websafe_string(self.request.get("c"))如果光标:q = Testing.query()q_forward = q.order(Testing.key)q_reverse = q.order(-Testing.key)条形,next_cursor,更多 = q_forward.fetch_page(2, start_cursor=cursor)rev_cursor = cursor.reversed()bar1, prev_cursor, more1 = q_reverse.fetch_page(2, start_cursor=rev_cursor)self.response.write("<a href=\"?c="+prev_cursor.to_websafe_string()+"\">&laquo</a><br/>")别的:q = Testing.query()q_forward = q.order(Testing.key)q_reverse = q.order(-Testing.key)条形,next_cursor,更多 = q_forward.fetch_page(2)self.response.write('Hello world!<br/>')酒吧中的酒吧:self.response.write(bar.t + "
")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)

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

解决方案

问题是您使用 c 来引用一个向后和向前的光标.

所以,当你得到一个已经被反转的向后光标时,你正在调用

rev_cursor = cursor.reversed()

之前

bars1, prev_cursor, more1 = q_reverse.fetch_page(2, start_cursor=rev_cursor)

因此在开始查询之前光标开始指向另一个方向.

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

 from google.appengine.ext import ndb类测试(ndb.Model):t = ndb.StringProperty()ndb.put_multi([测试(t='a'), 测试(t='b'), 测试(t='c')])

通过查询前两个元素获取游标:

<预><代码>>>>q_forward = Testing.query().order(Testing.t)>>>结果,forward_cursor,_ = q_forward.fetch_page(2)>>>打印 [value.t for value in result][u'a', u'b']

反向查询中使用该游标而不将其反向

<预><代码>>>>reverse_cursor = forward_cursor.reversed()>>>结果, _, _ = q_reverse.fetch_page(2, start_cursor=reverse_cursor)>>>打印 [value.t for value in result][u'b', u'a']

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

i have tried to make a backward paging, based on the example of google appengine documentation

NDB Query Cursor

my question is focused on this example:

# 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)

based on this example i create my own version (like this):

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()+"\">&laquo</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()+"\">&raquo</a>")

but i still not understand why, it cannot back to previous page perfectly... when i click forward page 1 to page 2:

Page 1 : a, b Page 2 : c, d

but when i click backward:

Page 2: c, d Page 1: b, c (should be: a, b)

it confused me a lot, since somebody at forum, can make it work based on this example, and nobody give an example code from them...

解决方案

The issue is that you are using c to refer to a backwards and forwards cursor.

So, when you get a backwards cursor which has already been reversed, you are calling

rev_cursor = cursor.reversed()

before

bars1, prev_cursor, more1 = q_reverse.fetch_page(2, start_cursor=rev_cursor)

and so the cursor begins pointing in the other direction before beginning the query.

To see a simpler test of this, define the same model and pre-populate some data:

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')])

get a cursor by querying for the first two elements:

>>> 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']

use that cursor in a reverse query without reversing it

>>> 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']

versus in a reverse query after doing so:

这篇关于python向后分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆