如何使用cursor()进行分页?

发布于 2024-10-15 02:41:26 字数 3170 浏览 5 评论 0 原文

谁能告诉我如何使用 Cursor() 进行分页?

我不清楚如何使用 cursor() rel="nofollow noreferrer">文档

这是我的查询:

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

谢谢!

更新

阿米尔您好:感谢您的回答,但我无法使此链接正常工作。这就是我所拥有的:

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

Can anyone point me to a practical application of cursor() to do pagination?

I am not clear how to use cursor() as given in the documentation.

This is my query:

items = db.GqlQuery("SELECT * FROM Item ORDER BY date DESC LIMIT 30")
                    

which I render like this:

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

Thanks!

UPDATE

Hi Amir: Thanks for your answer but I could't make this link work. Here's what I have:

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

But this results in this url which displays nothing:

http://localhost:8083/dir?type=newest?cursor=E9oBdgoTc2FyYWgtZm9yLXByZXNpZGVudBoESXRlbUtSBGRhdGVYAkwhQ1VSU09SIWoiahNzYXJhaC1mb3ItcHJlc2lkZW50cgsLEgRJdGVtGKsCDHIVCAcaBGRhdGUgACoJCMid8OXW4qYCggELCxIESXRlbRirAgzgAQAU

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏剧牡丹亭 2024-10-22 02:41:26

这是一个帮助您入门的简单示例......

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)

Here's a simple example to get you started...

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文