简体   繁体   English

通过谷歌应用引擎和python中的列表进行分页

[英]paging through a list in google app engine and python

i have a list. 我有一份清单。 (the list comes from a database query that is stored in memcache, ideally i will not query the database again.) (该列表来自存储在memcache中的数据库查询,理想情况下我不会再次查询数据库。)

x = [thing1, thing2, thing3, ... thing100]

i'm using this function to generate the first 10 items in the list at a time. 我正在使用此功能一次生成列表中的前10个项目。

def grouper(page_size, iterable):
        page = []
    for item in iterable:
        page.append(item)
        if len(page) == page_size:
            yield page
            page = []
    yield page

i'm calling the function 我在调用这个函数

group = grouper(10, x)

i submit my result to my html like so 我将结果提交给我的html就像这样

self.render("index.html", x-items = group.next())

in jinja2 i'm iterating through the group.next() and it correctly displays the first 10 items in the list. 在jinja2中,我正在遍历group.next()并正确显示列表中的前10个项目。

my question is, what code do i put in my html('next' link) to get it it to run the function again, and display the next 10 items? 我的问题是,我在我的html('下一个'链接)中添加了什么代码来让它再次运行该功能,并显示接下来的10个项目?

When you query the database and you need paging, you use cursors. 查询数据库并且需要分页时,使用游标。 Both the datastore and NDB support cursors. 数据存储区和NDB都支持游标。 So there is no need to save the query first in memcache. 因此,无需先在memcache中保存查询。

But if you need to store it first in memcache, you can use your own cursor. 但是如果你需要先将它存储在memcache中,你可以使用自己的光标。 See also this blog post from Nick Johnson: http://blog.notdot.net/2010/02/New-features-in-1-3-1-prerelease-Cursors 另请参阅Nick Johnson的博客文章: http//blog.notdot.net/2010/02/New-features-in-1-3-1-prerelease-Cursors

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM