diff options
| author | daniel a rios <misterrios@gmail.com> | 2019-04-24 22:51:47 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-06-24 11:47:56 +0200 |
| commit | 76b993a117b61c41584e95149a67d8a1e9f49dd1 (patch) | |
| tree | 317074f163cacbb137ec01ff6c7f39a76dc6fcf2 | |
| parent | d640c71fa35640b4c13107c074be2f52c52ad861 (diff) | |
Fixed #26431 -- Prevented django.urls.resolve() from returning missing optional parameters.
Previous behavior was inconsistent with django.urls.reverse() and
caused that translate_url() created an incorrect URL when an optional
parameter was missing.
| -rw-r--r-- | django/urls/resolvers.py | 2 | ||||
| -rw-r--r-- | tests/i18n/patterns/tests.py | 4 | ||||
| -rw-r--r-- | tests/urlpatterns/tests.py | 16 |
3 files changed, 17 insertions, 5 deletions
diff --git a/django/urls/resolvers.py b/django/urls/resolvers.py index af0508f94e..247e3680c0 100644 --- a/django/urls/resolvers.py +++ b/django/urls/resolvers.py @@ -153,7 +153,7 @@ class RegexPattern(CheckURLMixin): # If there are any named groups, use those as kwargs, ignoring # non-named groups. Otherwise, pass all non-named arguments as # positional arguments. - kwargs = match.groupdict() + kwargs = {k: v for k, v in match.groupdict().items() if v is not None} args = () if kwargs else match.groups() return path[match.end():], args, kwargs return None diff --git a/tests/i18n/patterns/tests.py b/tests/i18n/patterns/tests.py index 8e77d3fd8f..866bcb0bb8 100644 --- a/tests/i18n/patterns/tests.py +++ b/tests/i18n/patterns/tests.py @@ -160,6 +160,10 @@ class URLTranslationTests(URLTestCaseBase): self.assertEqual(translation.get_language(), 'en') # URL with parameters. self.assertEqual( + translate_url('/en/with-arguments/regular-argument/', 'nl'), + '/nl/with-arguments/regular-argument/', + ) + self.assertEqual( translate_url('/en/with-arguments/regular-argument/optional.html', 'nl'), '/nl/with-arguments/regular-argument/optional.html', ) diff --git a/tests/urlpatterns/tests.py b/tests/urlpatterns/tests.py index 66212d7df8..214739f678 100644 --- a/tests/urlpatterns/tests.py +++ b/tests/urlpatterns/tests.py @@ -55,10 +55,18 @@ class SimplifiedURLTests(SimpleTestCase): self.assertEqual(match.route, '^regex/(?P<pk>[0-9]+)/$') def test_re_path_with_optional_parameter(self): - match = resolve('/regex_optional/1/2/') - self.assertEqual(match.url_name, 'regex_optional') - self.assertEqual(match.kwargs, {'arg1': '1', 'arg2': '2'}) - self.assertEqual(match.route, r'^regex_optional/(?P<arg1>\d+)/(?:(?P<arg2>\d+)/)?') + for url, kwargs in ( + ('/regex_optional/1/2/', {'arg1': '1', 'arg2': '2'}), + ('/regex_optional/1/', {'arg1': '1'}), + ): + with self.subTest(url=url): + match = resolve(url) + self.assertEqual(match.url_name, 'regex_optional') + self.assertEqual(match.kwargs, kwargs) + self.assertEqual( + match.route, + r'^regex_optional/(?P<arg1>\d+)/(?:(?P<arg2>\d+)/)?', + ) def test_path_lookup_with_inclusion(self): match = resolve('/included_urls/extra/something/') |
