4

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
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Zeynel
  • 13,145
  • 31
  • 100
  • 145

1 Answers1

7

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)
Amir
  • 4,131
  • 26
  • 36
  • Any ideas on how to make the 'Previous Page' link? :D – Sudhir Jonathan Jan 30 '11 at 20:40
  • 1
    The easiest way is to simple use the back button. Since cursor is part of the url, the url will tell the query where to start. If you are doing some sort of Ajax solution, you can store the previous cursors. There is more info here: http://stackoverflow.com/questions/4752931/pagination-techniques-using-google-app-engine – Amir Jan 30 '11 at 21:02
  • thanks for your answer but I could not make the link to next page to work. What am I doing wrong? I added an update with the code. Thanks again. – Zeynel Feb 06 '11 at 01:09
  • 1
    The second ? should be an & in the url – Amir Feb 06 '11 at 01:32