[1.10.x] Fixed #27402 -- Fixed incorrect LocaleMiddleware redirects with prefix_default_language=False.

Backport of b8a815e9df from master
This commit is contained in:
Krzysztof Urbaniak 2016-11-05 12:56:34 +01:00 committed by Tim Graham
parent 75de55f1f3
commit 81b5971b1c
4 changed files with 22 additions and 1 deletions

View File

@ -35,7 +35,10 @@ class LocaleMiddleware(MiddlewareMixin):
urlconf = getattr(request, 'urlconf', settings.ROOT_URLCONF)
i18n_patterns_used, prefixed_default_language = is_language_prefix_patterns_used(urlconf)
if response.status_code == 404 and not language_from_path and i18n_patterns_used:
if (response.status_code == 404 and not language_from_path and
i18n_patterns_used and prefixed_default_language):
# Maybe the language code is missing in the URL? Try adding the
# language prefix and redirecting to that URL.
language_path = '/%s%s' % (language, request.path_info)
path_valid = is_valid_path(language_path, urlconf)
path_needs_slash = (

View File

@ -23,3 +23,6 @@ Bugfixes
* Fixed a ``QuerySet.update()`` crash on SQLite when updating a
``DateTimeField`` with an ``F()`` expression and a ``timedelta``
(:ticket:`27544`).
* Prevented ``LocaleMiddleware`` from redirecting on URLs that should return
404 when using ``prefix_default_language=False`` (:ticket:`27402`).

View File

@ -1843,6 +1843,20 @@ class UnprefixedDefaultLanguageTests(SimpleTestCase):
response = self.client.get('/de-simple-page/')
self.assertEqual(response.content, b'Yes')
def test_no_redirect_on_404(self):
"""
A request for a nonexistent URL shouldn't cause a redirect to
/<defaut_language>/<request_url> when prefix_default_language=False and
/<default_language>/<request_url> has a URL match (#27402).
"""
# A match for /group1/group2/ must exist for this to act as a
# regression test.
response = self.client.get('/group1/group2/')
self.assertEqual(response.status_code, 200)
response = self.client.get('/nonexistent/')
self.assertEqual(response.status_code, 404)
@override_settings(
USE_I18N=True,

View File

@ -6,5 +6,6 @@ from django.utils.translation import ugettext_lazy as _
urlpatterns = i18n_patterns(
url(r'^(?P<arg>[\w-]+)-page', lambda request, **arg: HttpResponse(_("Yes"))),
url(r'^simple/$', lambda r: HttpResponse(_("Yes"))),
url(r'^(.+)/(.+)/$', lambda *args: HttpResponse()),
prefix_default_language=False,
)