Pagination for date-based views

 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. Class-Based AJAX fallback view by fahhem 3 years, 3 months ago
  2. themed_template_loader by amitu 5 years, 1 month ago
  3. A GET string modifier templatetag by cogat 5 years, 4 months ago
  4. X-Sendfile static file serve view by dokterbob 3 years, 6 months ago
  5. SearchableManager by stephen_mcd 4 years, 1 month ago

Comments

jnns (on March 29, 2010):

Thanks. Will use your snippet.

#

(Forgotten your password?)