Skip to content

Instantly share code, notes, and snippets.

@adamalton
Last active December 16, 2015 06:09
Show Gist options
  • Save adamalton/5389373 to your computer and use it in GitHub Desktop.
Save adamalton/5389373 to your computer and use it in GitHub Desktop.
Simple pagination example on Google App Engine
<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 %}
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