From 99b565ce9658612d8e9537358675c2dc2e7539a2 Mon Sep 17 00:00:00 2001 From: Michael Elsdoerfer Date: Sun, 21 Sep 2008 20:58:11 +0200 Subject: [PATCH] builtin pagination now handles errors and converts them to Http404s --- django_tables/tables.py | 7 ++++++- tests/test_basic.py | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/django_tables/tables.py b/django_tables/tables.py index b7035c7..f4fea4f 100644 --- a/django_tables/tables.py +++ b/django_tables/tables.py @@ -1,4 +1,6 @@ import copy +from django.http import Http404 +from django.core import paginator from django.utils.datastructures import SortedDict from django.utils.encoding import StrAndUnicode from django.utils.text import capfirst @@ -376,7 +378,10 @@ class BaseTable(object): def paginate(self, klass, *args, **kwargs): page = kwargs.pop('page', 1) self.paginator = klass(self.rows, *args, **kwargs) - self.page = self.paginator.page(page) + try: + self.page = self.paginator.page(page) + except paginator.InvalidPage, e: + raise Http404(str(e)) class Table(BaseTable): diff --git a/tests/test_basic.py b/tests/test_basic.py index 5b4e65a..51f8972 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -6,6 +6,7 @@ This includes the core, as well as static data, non-model tables. from math import sqrt from nose.tools import assert_raises from django.core.paginator import Paginator +from django.http import Http404 import django_tables as tables def test_declaration(): @@ -304,6 +305,10 @@ def test_pagination(): assert books.paginator.num_pages == 10 assert books.page.has_previous() == False assert books.page.has_next() == True + # exceptions are converted into 404s + assert_raises(Http404, books.paginate, Paginator, 10, page=9999) + assert_raises(Http404, books.paginate, Paginator, 10, page="abc") + # TODO: all the column stuff might warrant it's own test file def test_columns(): -- 2.26.2