Ian Wienand: Skipping pages with django.core.paginator
Here's a little snippet for compressing the length of long Django
pagination by just
showing context around the currently selected page. What I wanted was
the first few and last few pages always selectable with some context
pages around the currently selected page; e.g.
If that's what you're looking for, some variation on below may be of
use. In this approach, you build up a list of pages similar to the
paginator object page_range but with only the relevant pages
and the skip-markers identified.
Then somehow pass through the pages_to_show to your view (below I
added it to the paginator object passed) and use a template along
the lines of
If that's what you're looking for, some variation on below may be of
use. In this approach, you build up a list of pages similar to the
paginator object page_range but with only the relevant pages
and the skip-markers identified.
from django.core.paginator import Paginator
import unittest
class Pages:
def __init__(self, objects, count):
self.pages = Paginator(objects, count)
def pages_to_show(self, page):
# pages_wanted stores the pages we want to see, e.g.
# - first and second page always
# - two pages before selected page
# - the selected page
# - two pages after selected page
# - last two pages always
#
# Turning the pages into a set removes duplicates for edge
# cases where the "context pages" (before and after the
# selected) overlap with the "always show" pages.
pages_wanted = set([1,2,
page-2, page-1,
page,
page+1, page+2,
self.pages.num_pages-1, self.pages.num_pages])
# The intersection with the page_range trims off the invalid
# pages outside the total number of pages we actually have.
# Note that includes invalid negative and >page_range "context
# pages" which we added above.
pages_to_show = set(self.pages.page_range).intersection(pages_wanted)
pages_to_show = sorted(pages_to_show)
# skip_pages will keep a list of page numbers from
# pages_to_show that should have a skip-marker inserted
# after them. For flexibility this is done by looking for
# anywhere in the list that doesn't increment by 1 over the
# last entry.
skip_pages = [ x[1] for x in zip(pages_to_show[:-1],
pages_to_show[1:])
if (x[1] - x[0] != 1) ]
# Each page in skip_pages should be follwed by a skip-marker
# sentinel (e.g. -1).
for i in skip_pages:
pages_to_show.insert(pages_to_show.index(i), -1)
return pages_to_show
class TestPages(unittest.TestCase):
def runTest(self):
objects = [x for x in range(0,1000)]
p = Pages(objects, 10)
self.assertEqual(p.pages_to_show(0),
[1, 2, -1, 99, 100])
self.assertEqual(p.pages_to_show(1),
[1,2,3,-1,99,100])
self.assertEqual(p.pages_to_show(2),
[1,2,3,4,-1,99,100])
self.assertEqual(p.pages_to_show(3),
[1,2,3,4,5,-1,99,100])
self.assertEqual(p.pages_to_show(4),
[1,2,3,4,5,6,-1,99,100])
self.assertEqual(p.pages_to_show(5),
[1,2,3,4,5,6,7,-1,99,100])
self.assertEqual(p.pages_to_show(6),
[1,2,-1,4,5,6,7,8,-1,99,100])
self.assertEqual(p.pages_to_show(7),
[1,2,-1,5,6,7,8,9,-1,99,100])
self.assertEqual(p.pages_to_show(50),
[1,2,-1,48,49,50,51,52,-1,99,100])
self.assertEqual(p.pages_to_show(93),
[1,2,-1,91,92,93,94,95,-1,99,100])
self.assertEqual(p.pages_to_show(94),
[1,2,-1,92,93,94,95,96,-1,99,100])
self.assertEqual(p.pages_to_show(95),
[1,2,-1,93,94,95,96,97,-1,99,100])
self.assertEqual(p.pages_to_show(96),
[1,2,-1,94,95,96,97,98,99,100])
self.assertEqual(p.pages_to_show(97),
[1,2,-1,95,96,97,98,99,100])
self.assertEqual(p.pages_to_show(98),
[1,2,-1,96,97,98,99,100])
self.assertEqual(p.pages_to_show(99),
[1,2,-1,97,98,99,100])
self.assertEqual(p.pages_to_show(100),
[1,2,-1,98,99,100])
if __name__ == '__main__':
unittest.main()
<ul class="pagination">
% if pages.has_previous %
<li><a href="foo.html?page= pages.previous_page_number ">«</a></li>
% else %
<li class="disabled"><a href="#">«</a></li>
% endif %
% for page in pages.pages_to_show %
% if page == -1 %
<li class="disabled"><a href="#">…</a></li>
% elif page == pages.number %
<li class="active"><a href="#"> page_num </a></li>
% else %
<li><a href="foo.html?page= page_num "> page_num </a>
% endif %
% endfor %
% if pages.has_next %
<li><a href="foo.html?page= pages.next_page_number ">»</a></li>
% else %
<li class="disabled"><a href="#">»</a></li>
% endif %
</ul>
I used to use
I have just pushed our pseudo-monthly batch of keyring updates to Debian. I am happy to inform you that, while the situation described in
This weekend I received my 17th certification (or Statement of
Accomplishment) for MOOCs. In particular, this last MOOC that I took was
Of course, my interest also made me want to streamline the build process and
use the command line tools (with ant, another tool that was "alien" to me,
having used makefiles for everything that I needed).
The use of Eclipse (and the Android development environment in general) in
my Debian unstable computers
I was pretty knackered today. I should have done some Debian stuff, but I just
didn't have it in me, and I had a backlog of household stuff to get done.
Zoe woke up at 4am, and I didn't have the energy to try and get her to go back
to sleep in her bed, so I just let her sleep in my bed. We both slept in until
7am, which was nice.
As a result, we didn't have as much time to have a slow start, and Zoe was a
bit grumpy and uncooperative as a result. I think there was at least one
meltdown before breakfast.
It conveniently rained around the time we were ready to leave, so we drove to
Kindergarten. Drop off was super smooth. She pretty much waved me off as soon
as we got there.
I picked up a couple of packages from the post office on the way home, and then
started hanging out the washing before I had to go back to see the podiatrist
to get my orthotics fitted in my new running shoes. I finished hanging out the
washing and putting away stuff from the Coochie trip and Melbourne trip and had
some lunch.
After lunch I started work on making a little step for Zoe so she can turn the
light off in her bedroom. I had enough material left over from the clothes
lines I made for her to make a really dodgy little "stool".
It rained again around pick up time, so I drove to Kindergarten again, and
picked up Zoe. She'd just woken up from a nap before I got there and was in a
good mood. Megan's Dad was picking up Megan on foot, and they were going to
have a coffee at the local coffee shop, so we joined them.
After that, we went to the post office to check my post office box. I had a
cheque that needed to be banked, so we went to the bank and the supermarket,
and by then it was pretty much time for Sarah to pick up Zoe from me.
I did a bit more carpentry before I lost the light, and went to yoga.

My hard-working KDE friend Mario asked me to help him to get Debian people to help him with his thesis. Here is what he writes:
Dear Free Software contributor*
I m currently in the process of writing my diploma thesis. I ve worked hard during the last few weeks and months on a
First step will be getting a NetBSD ISO image for installation
purpose. It can be downloaded from
If you can read this, that means my transition to
Moving from Lilo to GRUB, using LVM as default, etc throughout the last years it was time to evaluate how well LVM works without a separate boot partition, possibly also on top of Software RAID. Big disks are asking for partitioning with GPT, just UEFI isn t my default yet, so I m still defaulting to Legacy BIOS for Debian/wheezy (I expect this to change for Debian/jessie and according hardware approaching at my customers).
So what we have and want in this demonstration setup:
Following my
It seems that several people replied to the effect that they would pay people to take care of applying security updates, or even configuring adhoc things such as wikis, graphite, and MySQL.
Not enough people to rely upon, but perhaps there is scope for remote stuff being done in exchange for folding-money. (Of course some of those that replied are in foreign countries which makes receiving payment an annoyance, that's a separate problem though.)
Food for thought.
In the meantime I've settled into my use of 