diff --git a/django/urls/resolvers.py b/django/urls/resolvers.py index 0208601f799..78882c1bd4a 100644 --- a/django/urls/resolvers.py +++ b/django/urls/resolvers.py @@ -11,6 +11,7 @@ import functools import re from importlib import import_module +from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.utils import lru_cache, six from django.utils.datastructures import MultiValueDict @@ -388,7 +389,7 @@ class LocaleRegexURLResolver(RegexURLResolver): @property def regex(self): - language_code = get_language() + language_code = get_language() or settings.LANGUAGE_CODE if language_code not in self._regex_dict: regex_compiled = re.compile('^%s/' % language_code, re.UNICODE) self._regex_dict[language_code] = regex_compiled diff --git a/tests/i18n/patterns/tests.py b/tests/i18n/patterns/tests.py index c533a57204b..c503a74d346 100644 --- a/tests/i18n/patterns/tests.py +++ b/tests/i18n/patterns/tests.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals import os +from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.http import HttpResponsePermanentRedirect from django.middleware.locale import LocaleMiddleware @@ -75,6 +76,8 @@ class URLPrefixTests(URLTestCaseBase): self.assertEqual(reverse('prefixed'), '/en/prefixed/') with translation.override('nl'): self.assertEqual(reverse('prefixed'), '/nl/prefixed/') + with translation.override(None): + self.assertEqual(reverse('prefixed'), '/%s/prefixed/' % settings.LANGUAGE_CODE) @override_settings(ROOT_URLCONF='i18n.patterns.urls.wrong') def test_invalid_prefix_use(self):