Last active
December 16, 2015 06:09
-
-
Save adamalton/5389373 to your computer and use it in GitHub Desktop.
Simple pagination example on Google App Engine
This file contains 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
<h1>Page 1</h1> | |
<a href="/myview/?cursor={{cursor_for_next_page}}">Next page</a> | |
{% for object in results %} | |
{{object}} {#whatever you're displaying #} | |
{% endfor %} |
This file contains 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
from djangoappengine.db.utils import get_cursor, set_cursor | |
def my_page(request): | |
""" View some objects. """ | |
results_per_page = 100 | |
queryset = MyModel.objects.all() | |
#Note, it hasn't called the datastore yet as querysets are lazy | |
cursor = request.GET.get('cursor') | |
if cursor: | |
queryset = set_cursor(queryset, cursor) | |
results = queryset[0:results_per_page] #starts at the offset marked by the cursor | |
cursor_for_next_page = get_cursor(results) | |
template_vars = { | |
"results": results, | |
"cursor_for_next_page": cursor_for_next_page, | |
} | |
return render_to_response("template.html", template_vars) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment