Changeset - 5c7710954b2e
[Not reviewed]
0 3 0
Bradley Kuhn (bkuhn) - 9 years ago 2015-03-04 20:08:39
bkuhn@ebb.org
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.
3 files changed with 30 insertions and 6 deletions:
0 comments (0 inline, 0 general)
www/conservancy/apps/news/urls.py
Show inline comments
...
 
@@ -46,3 +46,3 @@ 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)),
 
)
www/conservancy/apps/news/views.py
Show inline comments
...
 
@@ -5,2 +5,3 @@ 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
...
 
@@ -19,2 +20,25 @@ class NewsListView(ListView):
 
                                    
 
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):
www/conservancy/templates/news/pressrelease_list.html
Show inline comments
...
 
@@ -15,5 +15,5 @@
 

	
 
{% 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">
...
 
@@ -30,5 +30,5 @@
 
<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>
0 comments (0 inline, 0 general)