Sign in

Google Cookbook - Google App Engine

The new Paginator :: paging logic simplified
Posted by osrdg.information on Sun 09 Nov 2008 in Django
User ratings:
If you are not very comfortable with the ObjectPaginator, you may give a try with the new Paginator.
Paginator is a thorough rework on the ObjectPaginator done by Django, and we find it more programmer friendly.

# imports 
from django import http
from django import shortcuts
from django.core.paginator import Paginator, InvalidPage, EmptyPage
# your sample class
class Contribution(db.Model):
contribution_from=db.UserProperty()
contribution_text=db.TextProperty()

# your http response helper class
def respond(request, user, template, params=None):
if params is None:
params = {}
if not template.endswith('.html'):
template += '.html'
return shortcuts.render_to_response(template, params)
# Code for Contribution
def contributionindex(request):
user = users.GetCurrentUser()
contribution_list = db.GqlQuery('SELECT * FROM Contribution ')
paginator = Paginator(contribution_list, 5) # Show 5 contributions per page
# Make sure page request is an int. If not, deliver first page.
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
# If page request is out of range, deliver last page of results.
try:
records = paginator.page(page)
except (EmptyPage, InvalidPage):
records = paginator.page(paginator.num_pages)

values = {
'records' : records ,
}

return respond(request, user, 'contribution', values )

# contribution.html is attached




NOTES:
1. A corresponding html with logic for paging is attached.
2. If you are using 0.96 with GAE this code will error out on your SDK; update your Django when it goes beyond 1.0
3. Or you know better, get the Paginator class locally for testing , until you upgrade your Django.
4. Refer to http://docs.djangoproject.com/en/dev/topics/pagination/?from=olddocs, for detailed discussion
KNOWN ISSUES:
1. App Engine limits the result set to 1000 entities against single execution of a GQL query. This recipe is bound by it.
Attached files
contribution.txtViewDownload

Comments (3)

Sign in to add comment.
1
thanks! recipes like this for integrating django with app engine are always welcome.

as mentioned elsewhere, it's worth noting that offset doesn't scale. we've set a fairly low cap on it for exactly that reason. happily, the new __key__ filters make it possible, if not trivial, to do scalable paging. see http://groups.google.com/group/google-appengine/browse_thread/thread/ee5afbde20e13cde
Comment by ryanb+appengine@google.com on Mon 26 Jan 2009 6:27 p.m.
2
Hello, On your SDK/django installation , search for a file named paginator.py. This file defines the classes which can be imported. Paginator is documented only on the latest version of django. Use the reference given in the recipe more details. Regards
Comment by osrdg.information on Fri 02 Jan 2009 3:51 a.m.
3
My django is updated to 1.0 and gae sdk is the newest. But I got the error:
cannot import name Paginator
Comment by coderzh on Sun 14 Dec 2008 11:37 a.m.