【问题标题】:App Engine datastore paging - previous pageApp Engine 数据存储区分页 - 上一页
【发布时间】:2016-01-16 05:44:20
【问题描述】:

我正在尝试创建一种分页机制来分页查询。前进不是问题。不过,转到上一页似乎并非易事。

我目前有的是这样的(为什么我有一种应该更简单的感觉?):

cursor_urlsafe = self.request.get('cursor', None)
rev = bool(self.request.get('rev', None))
cursor = ndb.Cursor(urlsafe=cursor_urlsafe)
if rev:
    next_query = Shot.query(Shot.schedule_key == schedule.key).order(Shot.date_created)
    cursor = cursor.reversed()
else:
    next_query = Shot.query(Shot.schedule_key == schedule.key).order(-Shot.date_created)

shots, next_cursor, more = next_query.fetch_page(PAGE_LENGTH, start_cursor=cursor)

if rev:
    shots.sort(key=lambda x: -x.date_created_epoch)

next_cursor_url_safe = next_cursor.urlsafe() if next_cursor else None
template_params = {
    'shots': shots,
    'next_cursor': next_cursor_url_safe,
    'prev_cursor': next_cursor_url_safe if cursor_urlsafe else None
}

在客户端:

<a href="/schedule/{{ s.id }}?cursor={{ prev_cursor }}&rev=true">Previous</a><br>
<a href="/schedule/{{ s.id }}?cursor={{ next_cursor }}">Next</a>

这类作品。唯一的问题是,当用户“改变方向”(返回页面)时,它会将他带到他所在的页面,并且只有在再次单击上一个后才会转到上一个页面。

所以如果点击,Previous 然后 Next,我会一直停留在同一个页面上。

请指教。

【问题讨论】:

    标签: python google-app-engine google-cloud-datastore


    【解决方案1】:

    您可以采用一些策略。存储以前的光标 - 请参阅 https://p.ota.to/blog/2013/4/pagination-with-cursors-in-the-app-engine-datastore/ 或交替搜索 SO 以寻找另一种方法 What is the correct way to get the previous page of results given an NDB cursor?

    【讨论】:

    • 如果我错了,请纠正我,但如果我需要“采用策略”以实现简单的分页功能,那感觉就像一个主要的设计失败了:(
    • 您正在处理无 SQL 环境。我会用谷歌解决“设计缺陷”。但是,这两个链接都为您提供了解决方案。您必须跳出 SQL 世界,了解这里发生了什么。查看 AWS 上的 DynamoDB 反向分页需要类似的策略。见stackoverflow.com/questions/14396346/pagination-in-dynamodb
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多