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.
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