Fixed #19240 -- include pagination error details in ListView 404

Thanks to seawolf for the patch
This commit is contained in:
Preston Holmes 2012-11-04 15:46:30 -08:00
parent 0a49e6164c
commit 5a00a57aa5
2 changed files with 15 additions and 4 deletions

View File

@ -50,9 +50,10 @@ class MultipleObjectMixin(ContextMixin):
try: try:
page = paginator.page(page_number) page = paginator.page(page_number)
return (paginator, page, page.object_list, page.has_other_pages()) return (paginator, page, page.object_list, page.has_other_pages())
except InvalidPage: except InvalidPage as e:
raise Http404(_('Invalid page (%(page_number)s)') % { raise Http404(_('Invalid page (%(page_number)s): %(message)s') % {
'page_number': page_number 'page_number': page_number,
'message': e.message,
}) })
def get_paginate_by(self, queryset): def get_paginate_by(self, queryset):

View File

@ -2,6 +2,7 @@ from __future__ import absolute_import
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings
from django.views.generic.base import View from django.views.generic.base import View
from .models import Author, Artist from .models import Author, Artist
@ -171,8 +172,17 @@ class ListViewTests(TestCase):
with self.assertNumQueries(3): with self.assertNumQueries(3):
self.client.get('/list/authors/notempty/paginated/') self.client.get('/list/authors/notempty/paginated/')
@override_settings(DEBUG=True)
def test_paginated_list_view_returns_useful_message_on_invalid_page(self):
# test for #19240
# tests that source exception's message is included in page
self._make_authors(1)
res = self.client.get('/list/authors/paginated/2/')
self.assertEqual(res.status_code, 404)
self.assertEqual(res.context.get('reason'),
"Invalid page (2): That page contains no results")
def _make_authors(self, n): def _make_authors(self, n):
Author.objects.all().delete() Author.objects.all().delete()
for i in range(n): for i in range(n):
Author.objects.create(name='Author %02i' % i, slug='a%s' % i) Author.objects.create(name='Author %02i' % i, slug='a%s' % i)