Skip to content

Instantly share code, notes, and snippets.

@mdornseif
Created October 2, 2010 16:03
Show Gist options
  • Save mdornseif/607757 to your computer and use it in GitHub Desktop.
Save mdornseif/607757 to your computer and use it in GitHub Desktop.
Shows how to realize simple pagination on Appengine
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
class Homepage(webapp.RequestHandler):
def render(self, values, template_name):
path = os.path.join(os.path.dirname(__file__), template_name)
self.response.out.write(template.render(path, values))
def paginate(self, query):
start = self.request.get_range('start', min_value=0, max_value=1000, default=0)
count = self.request.get_range('count', min_value=1, max_value=100, default=10)
# include one more story to see if we have another page, it is faster
# to include one more story if it is there, and then throw it away for
# the page, than issue a second query to test if there are more stories
objects = query.fetch(count+1, start)
# was the extra object retrieved? If so, there are more stories to go
more_objects = len(objects) > count
objects = objects[:count]
# if this is not a 0 start, there are previous objects too
prev_objects = start > 0
# and pass in the actual number of objects that will be displayed
prev_start = max(start-count-1, 0)
next_start = start + len(rechnungen) - 1
return dict(objects=objects,
more_objects=more_objects, prev_objects=prev_objects,
prev_start=prev_start, next_start=next_start)
def get(self): # based on http://bit.ly/9kRSw4
# Query your model you want to paginate here
query = models.Rechnung.all().filter('kdnnr =', 'SC66669').order('-leistungsdatum')
values = self.paginate(query)
self.render(values, 'pageinated.html')
<style type="text/css">
.pagination_prev { float: left; }
.pagination_next { float: right; }
</style>
<table>
{% for object in objects %}
<tr><td>{{ object }}</td></tr>
{% endfor %}
</table>
<div class="pagination">
{% if prev_rechnungen %}<a class="pagination_prev" href="?start={{ prev_start }}">
← zurück</a>{% endif %}
{% if more_rechnungen %}<a class="pagination_next" href="?start={{ next_start }}">
mehr →</a>{% endif %}
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment