title: How to use cursor() for pagination?
body: <p>Can anyone point me to a practical application of <code>cursor()</code> to do pagination?</p>
  <p>I am not clear how to use <code>cursor()</code> as given in the <a href="https://code.google.com/appengine/docs/python/datastore/gqlqueryclass.html#GqlQuery_cursor" rel="nofollow noreferrer">documentation</a>.</p>
  <p>This is my query:</p>
  <pre><code>items = db.GqlQuery(&quot;SELECT * FROM Item ORDER BY date DESC LIMIT 30&quot;)
                      
  </code></pre>
  <p>which I render like this:</p>
  <pre><code>self.response.out.write(&quot;&lt;ol&gt;&quot;)
  for item in items:
      self.response.out.write(&quot;&quot;&quot;&lt;li&gt;&lt;a href=&quot;/vote/%s?type=%s&quot;&gt; ^ &lt;/a&gt;&lt;a href=&quot;%s&quot;&gt;%s&lt;/a&gt; &lt;span id='Small'&gt;(%s)&lt;/span&gt;&lt;br /&gt; 
          &lt;div id='Small'&gt; 
          %s points %s by %s &lt;a href=&quot;/item/%s&quot;&gt;&lt;/a&gt; | 
          &lt;a href=&quot;/item/%s#disqus_thread&quot;&gt;&lt;/a&gt;&lt;/div&gt;
          &lt;/li&gt;&lt;br /&gt; &quot;&quot;&quot; % 
          (str(item.key().id()), merchandise_type, item.url, item.title, urlparse(item.url).netloc,
          item.points, item.date.strftime(&quot;%B %d, %Y %I:%M%p&quot;), item.user_who_liked_this_item, str(item.key().id()), str(item.key().id())))                               
                                          
     self.response.out.write(&quot;&lt;/ol&gt;&quot;)
  </code></pre>
  <p>Thanks!</p>
  <p><strong>UPDATE</strong></p>
  <p>Hi Amir: Thanks for your answer but I could't make this link work. Here's what I have:</p>
  <pre><code>#===========adding cursor here===========#
              cursor = self.request.get(&quot;cursor&quot;)
              if cursor: query.with_cursor(cursor)
              items = query.fetch(30)
              cursor  = query.cursor()
          
  #===========adding cursor here===========#
  
  #===========regular output===========#
              self.response.out.write(&quot;&lt;ol&gt;&quot;)
              for item in items:
                  self.response.out.write(&quot;&quot;&quot;&lt;li&gt;
                  &lt;a href=&quot;/vote/%s?type=%s&quot;&gt; ^ &lt;/a&gt;&lt;a href=&quot;%s&quot;&gt;
                  &lt;span id=&quot;large&quot;&gt;%s&lt;/span&gt;&lt;/a&gt; 
                  &lt;span id='Small'&gt;(%s)&lt;/span&gt;
                  &lt;br /&gt;  
                  %s&lt;br /&gt; &lt;span id='Small'&gt; 
                  %s points %s by %s &lt;a href=&quot;/item/%s&quot;&gt;&lt;/a&gt; | 
                  &lt;a href=&quot;/item/%s#disqus_thread&quot;&gt;&lt;/a&gt;
                  &lt;/span&gt;
                  &lt;/li&gt;&lt;br /&gt; &quot;&quot;&quot; %           
                  (str(item.key().id()), merchandise_type, item.url, 
                  item.title, urlparse(item.url).netloc, 
                  item.summary, item.points, item.date.strftime(&quot;%B %d, %Y %I:%M%p&quot;), 
                  item.user_who_liked_this_item, str(item.key().id()),  
                  str(item.key().id())))                               
                                          
              self.response.out.write(&quot;&lt;/ol&gt;&quot;)
  #===========regular output===========#
  
  #===========link to cursor===========#
              self.response.out.write(&quot;&quot;&quot;&lt;a href=&quot;/dir?type=%s?cursor=%s&quot;&gt;Next
              Page&lt;/a&gt;&quot;&quot;&quot; % (merchandise_type, cursor))
          
  </code></pre>
  <p>But this results in this url which displays nothing:</p>
  <pre><code>http://localhost:8083/dir?type=newest?cursor=E9oBdgoTc2FyYWgtZm9yLXByZXNpZGVudBoESXRlbUtSBGRhdGVYAkwhQ1VSU09SIWoiahNzYXJhaC1mb3ItcHJlc2lkZW50cgsLEgRJdGVtGKsCDHIVCAcaBGRhdGUgACoJCMid8OXW4qYCggELCxIESXRlbRirAgzgAQAU
  </code></pre>