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.
This commit is contained in:
Bradley M. Kuhn 2015-03-04 12:08:39 -08:00
parent f742f86f4b
commit 5c7710954b
3 changed files with 30 additions and 6 deletions

View file

@ -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)),
)

View file

@ -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

View file

@ -13,9 +13,9 @@
<h1><a href="/feeds/news/" class="feedlink"><img src="/img/feed-icon-14x14.png" alt="[RSS]"/></a> Conservancy News</h1>
{% ifnotequal page 1 %}<p>(page {{ page }} of {{ pages }})</p>{% endifnotequal %}
{% ifnotequal news.number 1 %}<p>(page {{ news.number }} of {{ news.paginator.num_pages }})</p>{% endifnotequal %}
{% for object in object_list %}
{% for object in news %}
<div class="shaded">
<p class="date">{{ object.pub_date|date:"F j, Y" }}</p>
<h3><a href="{{ object.get_absolute_url }}">{{ object.headline|safe }}</a></h3>
@ -28,9 +28,9 @@
{% endfor %}
<p>
{% if has_next %}<a class="next_page_button" href="?page={{ next }}">Next page (older) &raquo;</a>{% endif %}
{% if has_previous %}<a href="?page={{ previous }}">&laquo; Previous page (newer)</a>{% endif %}
{% comment %}{% for pagenum in page_navigation %}{% ifequal pagenum page %}[{{ pagenum }}]{% else %}<a href="?page={{ pagenum }}">{{ pagenum }}</a>{% endifequal %} {% endfor %}{% endcomment %}
{% if news.has_next %}<a class="next_page_button" href="?page={{ next }}">Next page (older) &raquo;</a>{% endif %}
{% if news.has_previous %}<a href="?page={{ previous }}">&laquo; Previous page (newer)</a>{% endif %}
{% comment %}{% for pagenum in news.paginator.page_range %}{% ifequal pagenum news.number %}[{{ pagenum }}]{% else %}<a href="?page={{ pagenum }}">{{ pagenum }}</a>{% endifequal %} {% endfor %}{% endcomment %}
</p>
</div>
</div>