Login

Pagination for date-based views

Author:
theju
Posted:
November 1, 2007
Language:
Python
Version:
1.2
Score:
4 (after 4 ratings)

Date-based generic views do not provide pagination by default but Django is very extensible. It provides a Paginator that takes care of pagination. Since date based views usually order by descending order ie from latest entry to the oldest, I used a queryset to order the items (on a field called 'date_pub') and then pass this queryset to the paginator which takes care of the pagination.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from django.core.paginator import Paginator
from django.views.generic.list_detail import object_list
from django.http import Http404
from django.shortcuts import get_list_or_404
from app_name.models import Model_Name

num_in_page = 5

# Pagination for the equivalent of archive_index generic view.
# The url is of the form http://host/page/4/
# In urls.py for example, ('^blog/page/(?P<page>\d)/$', get_archive_index),
def get_archive_index(request, page):
    queryset = Model_Name.objects.order_by("-date_pub")
    paginator = Paginator(queryset, num_in_page)
    if int(page) in paginator.page_range:
       return object_list(request, queryset, paginate_by=num_in_page, page=int(page))
    # send a 404 error that page is not found.
    return Http404

# Pagination for the equivalent of archive_year generic view.
# The URL is of the form http://host/2007/page/1/
# urls.py,say, ('^blog/(?P<year>\d{4})/page/(?P<page>\d)/$',get_archive_year),
def get_archive_year(request, year, page):
    queryset = get_list_or_404(Model_Name, date_pub__year=year)
    paginator = Paginator(queryset, num_in_page)   
    if int(page) in paginator.page_range:
       return object_list(request, queryset, paginate_by=num_in_page, page=int(page))
    return Http404

# Pagination for the equivalent of archive_month generic view.
# The URL is of the form http://host/2007/10/page/2/
# ('^blog/(?P<year>\d{4})/(?P<month>\d{2})/page/(?P<page>\d)/$',get_archive_month),
def get_archive_month(request, year, month, page):
    queryset = get_list_or_404(Model_Name, date_pub__year=year, date_pub__month=month)
    paginator = Paginator(queryset, num_in_page)   
    if int(page) in paginator.page_range:
       return object_list(request, queryset, paginate_by=num_in_page, page=int(page))
    return Http404

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

jnns (on March 29, 2010):

Thanks. Will use your snippet.

#

Please login first before commenting.