Created
October 2, 2010 16:03
-
-
Save mdornseif/607757 to your computer and use it in GitHub Desktop.
Shows how to realize simple pagination on Appengine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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