How to use cursor() for pagination?

2019-01-23 21:01发布

问题:

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

Update 2

See @Amir comment below: changing the second ? to & solved the problem. Thanks!

回答1:

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)