How to use cursor () to paginate?

Can someone point me to the practical use of cursor() for pagination?

I do not understand how to use cursor() as indicated in the documentation .

This is my request:

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

which I do 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: Thank you for your answer, but I could not get this link to work. Here is 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 leads to this url that doesn't display anything:

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

Update 2

See @Amir comment below: change second ? on & solved the problem. Thanks!

+4
source share
1 answer

Here is 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) 
+7
source

Source: https://habr.com/ru/post/1337631/


All Articles