From 8f4c8a69d6f80b8dbad8518bbe4c3da1c492ec8a 2015-03-04 23:04:13 From: Bradley M. Kuhn Date: 2015-03-04 23:04:13 Subject: [PATCH] Rework blog's custom_index for new pagination. The pagination support changed, and as was previously done a few commits ago for news, I'm trying a similar solution for blogs. In this case, I'm trying to use the existing custom_index() method we have and adapt it to properly support pagination in the way we want. I'm not completely sure this will work, but I think it's at least close. --- diff --git a/www/conservancy/apps/blog/views.py b/www/conservancy/apps/blog/views.py index 7a95819ac8024373f6e9934d8241a52808e3b056..829c0233ea07d476e3efea858d3ad5094f2454ba 100644 --- a/www/conservancy/apps/blog/views.py +++ b/www/conservancy/apps/blog/views.py @@ -64,6 +64,24 @@ def custom_index(request, queryset, *args, **kwargs): # return object_list(request, queryset, *args, **kwargs) kwargs['queryset'] = queryset kwargs['extra_context'] = extra_context + + paginator = Paginator(queryset, paginate_by) + + page = request.GET.get('page') + + try: + blog_entries = paginator.page(page) + except PageNotAnInteger: + # If page is not an integer, deliver first page. + blog_entries = paginator.page(1) + except EmptyPage: + # If page is out of range (e.g. 9999), deliver last page of results. + blog_entires = paginator.page(paginator.num_pages) + + return render_to_response('blog/entry_list.html', + {"blog_entries": blog_entries, "date_list" : date_list, + "authors" : authors, "tags" : tags }) + callable = BlogListView.as_view(**kwargs) return callable(request) diff --git a/www/conservancy/templates/blog/entry_list.html b/www/conservancy/templates/blog/entry_list.html index 29f1adb3c256ef9f2247f09d3ee1edab56a1bb8b..e7c21317ac1e7bd0fa86d332755258d678c893dc 100644 --- a/www/conservancy/templates/blog/entry_list.html +++ b/www/conservancy/templates/blog/entry_list.html @@ -12,19 +12,21 @@

{% endif %} -{% for object in object_list %} +{% for entry in blog_entries %}
-

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

-

{{ object.headline|safe }}

- {{ object.body|safe }} -

Posted by {{ object.author.formal_name}} on {{ object.pub_date|date:"F j, Y" }}

- {% if object.tags.all %}

Tags: {% for tag in object.tags.all %}{{ tag.label }}{% if not forloop.last %}, {% endif %}{% endfor %}

{% endif %} +

{{ entry.pub_date|date:"F j, Y" }} by {{ entry.author.formal_name }}

+

{{ entry.headline|safe }}

+ {{ entry.body|safe }} +

Posted by {{ entry.author.formal_name}} on {{ entry.pub_date|date:"F j, Y" }}

+ {% if entry.tags.all %}

Tags: {% for tag in entry.tags.all %}{{ tag.label }}{% if not forloop.last %}, {% endif %}{% endfor %}

{% endif %}
{% endfor %}

-{% if has_next %}Next page (older) »{% endif %} -{% if has_previous %}« Previous page (newer){% endif %} +{% if blog_entries.has_next %}Next page (older) »{% endif %} +{% if blog_entries.has_previous %}« Previous page (newer){% endif %} +

+

{% for pagenum in blog_entries.paginator.page_range %}{% ifequal pagenum blog_entries.number %}[{{ pagenum }}]{% else %}{{ pagenum }}{% endifequal %} {% endfor %}