python向后分页 [英] python backward paging

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

问题描述

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



NDB查询光标



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

 #设置。 
q = Bar.query()
q_forward = q.order(Bar.key)
q_reverse = q.order(-Bar.key)

#网页前进。
bars,cursor,more = q_forward.fetch_page(10)

#获取相同的页面后退。
rev_cursor = cursor.reversed()
bars1,cursor1,more1 = q_reverse.fetch_page(10,start_cursor = rev_cursor)

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

  def get (self):
#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();

cursor = ndb.Cursor.from_websafe_string(self.request.get(c))

如果光标:
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 =测试。 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酒吧在酒吧:
self.response.write(bar.t +< br />)


self.response.write(< a href = \?c =+ next_cursor.to_websafe_string()+\>& raquo< / a>)

但我仍不明白为什么,它无法完美地回到上一页...
当我点击前页1到页面2时:



第1页:a,b
第2页:c,d p

但是当我点击后面时:



第2页:c,d
Page 1:b,c(应该是:a,b)

它使我困惑很多,因为有人在论坛上,可以使它基于这个例子工作,并没有人给他们的例子代码...

解决方案

问题是你使用 c 来引用一个向前和向前的游标。

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

$ $ $ $ $ $ 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测试(ndb.Model):
t = ndb.StringProperty()
ndb.put_multi (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']

与反向查询之后进行反向查询:


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天全站免登陆