有人能指出cursor()
实际应用分页吗?
我不清楚如何使用documentation中给出的cursor()
。
这是我的疑问:
items = db.GqlQuery("SELECT * FROM Item ORDER BY date DESC LIMIT 30")
我这样渲染:
self.response.out.write("<ol>")
for item in items:
self.response.out.write("""<li><a href="/vote/%s?type=%s"> ^ </a><a href="%s">%s</a> <span id='Small'>(%s)</span><br />
<div id='Small'>
%s points %s by %s <a href="/item/%s"></a> |
<a href="/item/%s#disqus_thread"></a></div>
</li><br /> """ %
(str(item.key().id()), merchandise_type, item.url, item.title, urlparse(item.url).netloc,
item.points, item.date.strftime("%B %d, %Y %I:%M%p"), item.user_who_liked_this_item, str(item.key().id()), str(item.key().id())))
self.response.out.write("</ol>")
谢谢!
更新
您好Amir:感谢您的回答,但我无法使此链接正常工作。这就是我所拥有的:
#===========adding cursor here===========#
cursor = self.request.get("cursor")
if cursor: query.with_cursor(cursor)
items = query.fetch(30)
cursor = query.cursor()
#===========adding cursor here===========#
#===========regular output===========#
self.response.out.write("<ol>")
for item in items:
self.response.out.write("""<li>
<a href="/vote/%s?type=%s"> ^ </a><a href="%s">
<span id="large">%s</span></a>
<span id='Small'>(%s)</span>
<br />
%s<br /> <span id='Small'>
%s points %s by %s <a href="/item/%s"></a> |
<a href="/item/%s#disqus_thread"></a>
</span>
</li><br /> """ %
(str(item.key().id()), merchandise_type, item.url,
item.title, urlparse(item.url).netloc,
item.summary, item.points, item.date.strftime("%B %d, %Y %I:%M%p"),
item.user_who_liked_this_item, str(item.key().id()),
str(item.key().id())))
self.response.out.write("</ol>")
#===========regular output===========#
#===========link to cursor===========#
self.response.out.write("""<a href="/dir?type=%s?cursor=%s">Next
Page</a>""" % (merchandise_type, cursor))
但是这会导致此网址没有显示任何内容:
http://localhost:8083/dir?type=newest?cursor=E9oBdgoTc2FyYWgtZm9yLXByZXNpZGVudBoESXRlbUtSBGRhdGVYAkwhQ1VSU09SIWoiahNzYXJhaC1mb3ItcHJlc2lkZW50cgsLEgRJdGVtGKsCDHIVCAcaBGRhdGUgACoJCMid8OXW4qYCggELCxIESXRlbRirAgzgAQAU
更新2
请参阅下面的@Amir评论:将第二个?
更改为&
解决了问题。谢谢!
答案 0 :(得分:7)
这是一个让你入门的简单例子......
query = db.GqlQuery("SELECT * FROM Item ORDER BY date DESC")
cursor = self.request.get('cursor')
if cursor: query.with_cursor(cursor)
items = query.fetch(30)
cursor = query.cursor()
... your regular output ...
self.response.out.write('<a href="yoururl?cursor=%s">Next Page</a>' % cursor)