Pagination is here! 08.06.2007

I mentioned in a previous post what I wanted the pagination for this site to look like — in essence, a stream of posts accessible via next/previous links at the bottom. I don’t like being forced to click on each month. It breaks the process of simply reading down the main column of posts.

That said, I’ve just added newer/older links at the bottom of each page. I chose “newer” and “older” because they denote a meaning that’s more clear in my mind than “next” and “previous”1.

The whole thing was incredibly easy with Django’s built in pagination. Here is the entirety of the view:

def pages(request, page):
    target = eval(page) - 1
    pages = ObjectPaginator(Entry.objects.all(), 7)
    return direct_to_template(request, template=’blog/index.html’, extra_context={
            ‘entries’: pages.get_page(target),
            ‘has_next’: pages.has_next_page(target),
            ‘next_page’: target + 2,
            ‘has_prev’: pages.has_previous_page(target),
            ‘prev_page’: target,
            ‘current_page’: page
        })

That’s it. It uses the same template file as the main page. It uses the same generic list view. I’m simply dividing up how the main list gets passed in.

It was easy. And awesome.


  1. I mean, what does “previous” and “next” really mean? By themselves, it doesn’t give any indication of what kind of posts to expect. A wise reader could surmise that beacuse the posts are listed in reverse chronological order, then the next page should have older posts, and the previous page should have newer posts, but that’s completely counter to how real world pages work. When you flip open a book, the “next” page is newer, and the “previous” page is older.