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\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\d{4})/page/(?P\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\d{4})/(?P\d{2})/page/(?P\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