From 5c7710954b2e0e914e0cf19f5a9476a923b472cb Mon Sep 17 00:00:00 2001 From: "Bradley M. Kuhn" Date: Wed, 4 Mar 2015 12:08:39 -0800 Subject: [PATCH] Attempt to use Django 1.7 Paginator object. Upon reading https://docs.djangoproject.com/en/1.7/topics/pagination/#using-paginator-in-a-view I wonder if we should simply follow this model for pagination of news items. I've attempted to adapt that herein. This is a first attempt and likely will have issues. --- www/conservancy/apps/news/urls.py | 2 +- www/conservancy/apps/news/views.py | 24 +++++++++++++++++++ .../templates/news/pressrelease_list.html | 10 ++++---- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/www/conservancy/apps/news/urls.py b/www/conservancy/apps/news/urls.py index 55a6f3f0..cb48e89e 100644 --- a/www/conservancy/apps/news/urls.py +++ b/www/conservancy/apps/news/urls.py @@ -44,5 +44,5 @@ urlpatterns = patterns('', ) urlpatterns += patterns('', - (r'^/?$', 'conservancy.apps.news.views.custom_index', dict(info_dict, paginate_by=6)), + (r'^/?$', 'conservancy.apps.news.views.listing', dict(info_dict, paginate_by=6)), ) diff --git a/www/conservancy/apps/news/views.py b/www/conservancy/apps/news/views.py index dec05a1d..0944f862 100644 --- a/www/conservancy/apps/news/views.py +++ b/www/conservancy/apps/news/views.py @@ -3,6 +3,7 @@ from django.views.generic import ListView from django.views.generic.dates import YearArchiveView, MonthArchiveView, DayArchiveView, DateDetailView from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from conservancy.apps.news.models import ExternalArticle +from conservancy.apps.news.models import PressRelease from conservancy.apps.events.models import Event from datetime import datetime # for debugging... @@ -17,6 +18,29 @@ class NewsListView(ListView): context.update(self.extra_context) return context +def listing(request): + news = PressRelease.objects.all() + +# if (not kwargs.has_key('allow_future')) or not kwargs['allow_future']: + news = news.filter(**{'%s__lte' % kwargs['date_field']: + datetime.now()}) + + date_list = news.dates(kwargs['date_field'], 'year') + + paginator = Paginator(news, 6) # Show 6 news items per page + + page = request.GET.get('page') + try: + contacts = paginator.page(page) + except PageNotAnInteger: + # If page is not an integer, deliver first page. + contacts = paginator.page(1) + except EmptyPage: + # If page is out of range (e.g. 9999), deliver last page of results. + contacts = paginator.page(paginator.num_pages) + + return render_to_response('pressrelease_list.html', {"news": news, "date_list" : date_list}) + def custom_index(request, queryset, *args, **kwargs): """News index. Calls a generic list view, but passes additional context including past and future events, and an index of news by diff --git a/www/conservancy/templates/news/pressrelease_list.html b/www/conservancy/templates/news/pressrelease_list.html index 350d1385..b0650b8d 100644 --- a/www/conservancy/templates/news/pressrelease_list.html +++ b/www/conservancy/templates/news/pressrelease_list.html @@ -13,9 +13,9 @@

[RSS] Conservancy News

-{% ifnotequal page 1 %}

(page {{ page }} of {{ pages }})

{% endifnotequal %} +{% ifnotequal news.number 1 %}

(page {{ news.number }} of {{ news.paginator.num_pages }})

{% endifnotequal %} -{% for object in object_list %} +{% for object in news %}

{{ object.pub_date|date:"F j, Y" }}

{{ object.headline|safe }}

@@ -28,9 +28,9 @@ {% endfor %}

-{% if has_next %}Next page (older) »{% endif %} -{% if has_previous %}« Previous page (newer){% endif %} -{% comment %}{% for pagenum in page_navigation %}{% ifequal pagenum page %}[{{ pagenum }}]{% else %}{{ pagenum }}{% endifequal %} {% endfor %}{% endcomment %} +{% if news.has_next %}Next page (older) »{% endif %} +{% if news.has_previous %}« Previous page (newer){% endif %} +{% comment %}{% for pagenum in news.paginator.page_range %}{% ifequal pagenum news.number %}[{{ pagenum }}]{% else %}{{ pagenum }}{% endifequal %} {% endfor %}{% endcomment %}