This is a function wrapping the code from example from django docs.
The required parameters are: request which is a Request object from a view, and objects - a list of objects to paginate.
You may want to tune number of items per page by specifying count and the name of a GET parameter through param_name
To use it in your view just wrap the paginated object into a function for example:
def someview(request):
articles = Article.objects.all()
... some other logics ...
return render_to_response(template, {'articles': paginate(request, articles)}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | def paginate(request, objects, count=10, param_name='page'):
"""
Shortcut for paginating by some object
"""
paginator = Paginator(objects, count)
try:
pagenum = int(request.GET.get('%s' % param_name, '1'))
except ValueError:
pagenum = 1
try:
result = paginator.page(pagenum)
except (EmptyPage, InvalidPage):
result = paginator.page(paginator.num_pages)
return result
|
More like this
- Form field with fixed value by roam 1 week, 5 days ago
- New Snippet! by Antoliny0919 2 weeks, 5 days ago
- Add Toggle Switch Widget to Django Forms by OgliariNatan 3 months, 1 week ago
- get_object_or_none by azwdevops 7 months ago
- Mask sensitive data from logger by agusmakmun 8 months, 3 weeks ago
Comments
Please login first before commenting.