[1.11.x] Fixed #28204 -- Fixed MultipleObjectMixin.paginate_queryset() crash on Python 2 if InvalidPage message contains non-ASCII.

This commit is contained in:
Tim Graham 2017-05-29 09:45:41 -04:00
parent 2c03e14586
commit c341803315
3 changed files with 13 additions and 1 deletions

View File

@ -5,6 +5,7 @@ from django.core.paginator import InvalidPage, Paginator
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from django.http import Http404 from django.http import Http404
from django.utils import six from django.utils import six
from django.utils.encoding import force_text
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.views.generic.base import ContextMixin, TemplateResponseMixin, View from django.views.generic.base import ContextMixin, TemplateResponseMixin, View
@ -80,7 +81,7 @@ class MultipleObjectMixin(ContextMixin):
except InvalidPage as e: except InvalidPage as e:
raise Http404(_('Invalid page (%(page_number)s): %(message)s') % { raise Http404(_('Invalid page (%(page_number)s): %(message)s') % {
'page_number': page_number, 'page_number': page_number,
'message': str(e) 'message': force_text(e),
}) })
def get_paginate_by(self, queryset): def get_paginate_by(self, queryset):

View File

@ -36,3 +36,6 @@ Bugfixes
* Relaxed the validation added in Django 1.11 of the fields in the ``defaults`` * Relaxed the validation added in Django 1.11 of the fields in the ``defaults``
argument of ``QuerySet.get_or_create()`` and ``update_or_create()`` to argument of ``QuerySet.get_or_create()`` and ``update_or_create()`` to
reallow settable model properties (:ticket:`28222`). reallow settable model properties (:ticket:`28222`).
* Fixed ``MultipleObjectMixin.paginate_queryset()`` crash on Python 2 if the
``InvalidPage`` message contains non-ASCII (:ticket:`28204`).

View File

@ -5,6 +5,7 @@ import datetime
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase, override_settings from django.test import TestCase, override_settings
from django.utils import translation
from django.utils.encoding import force_str from django.utils.encoding import force_str
from django.views.generic.base import View from django.views.generic.base import View
@ -103,6 +104,13 @@ class ListViewTests(TestCase):
res = self.client.get('/list/authors/paginated/42/') res = self.client.get('/list/authors/paginated/42/')
self.assertEqual(res.status_code, 404) self.assertEqual(res.status_code, 404)
def test_paginated_page_out_of_range_non_ascii_message(self):
msg = 'Ung\xfcltige Seite (42): Diese Seite enth\xe4lt keine Ergebnisse'
with translation.override('de'):
res = self.client.get('/list/authors/paginated/42/')
self.assertEqual(res.status_code, 404)
self.assertEqual(res.context['exception'], msg)
def test_paginated_invalid_page(self): def test_paginated_invalid_page(self):
self._make_authors(100) self._make_authors(100)
res = self.client.get('/list/authors/paginated/?page=frog') res = self.client.get('/list/authors/paginated/?page=frog')