From f0f5212624d7a3edbf77126e0a3060d38db501e1 Mon Sep 17 00:00:00 2001 From: Bas Peschier Date: Sun, 8 Mar 2015 18:26:30 +0100 Subject: [PATCH] Fixed #12943 -- Allowed unnamed arguments to be propagated in includes Propagated unnamed arguments as positional arguments into included URLconfs if no named arguments are defined. Positional and keyword arguments are never combined. --- django/core/urlresolvers.py | 10 +++++++++- tests/urlpatterns_reverse/namespace_urls.py | 1 + tests/urlpatterns_reverse/tests.py | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py index 42a9fbc2fe..76b12c1ee4 100644 --- a/django/core/urlresolvers.py +++ b/django/core/urlresolvers.py @@ -374,11 +374,19 @@ class RegexURLResolver(LocaleRegexProvider): tried.append([pattern]) else: if sub_match: + # Merge captured arguments in match with submatch sub_match_dict = dict(match.groupdict(), **self.default_kwargs) sub_match_dict.update(sub_match.kwargs) + + # If there are *any* named groups, ignore all non-named groups. + # Otherwise, pass all non-named arguments as positional arguments. + sub_match_args = sub_match.args + if not sub_match_dict: + sub_match_args = match.groups() + sub_match.args + return ResolverMatch( sub_match.func, - sub_match.args, + sub_match_args, sub_match_dict, sub_match.url_name, self.app_name or sub_match.app_name, diff --git a/tests/urlpatterns_reverse/namespace_urls.py b/tests/urlpatterns_reverse/namespace_urls.py index e1bb7479e9..d8a1dbc103 100644 --- a/tests/urlpatterns_reverse/namespace_urls.py +++ b/tests/urlpatterns_reverse/namespace_urls.py @@ -50,6 +50,7 @@ urlpatterns = [ url(r'^included/', include('urlpatterns_reverse.included_namespace_urls')), url(r'^inc(?P[0-9]+)/', include('urlpatterns_reverse.included_urls', namespace='inc-ns5')), + url(r'^included/([0-9]+)/', include('urlpatterns_reverse.included_namespace_urls')), url(r'^ns-outer/(?P[0-9]+)/', include('urlpatterns_reverse.included_namespace_urls', namespace='inc-outer')), diff --git a/tests/urlpatterns_reverse/tests.py b/tests/urlpatterns_reverse/tests.py index f7f8fcc3f8..7d08cd21e9 100644 --- a/tests/urlpatterns_reverse/tests.py +++ b/tests/urlpatterns_reverse/tests.py @@ -41,6 +41,7 @@ resolve_test_data = ( # Unnamed args are dropped if you have *any* kwargs in a pattern ('/mixed_args/42/37/', 'mixed-args', None, '', 'mixed-args', views.empty_view, tuple(), {'arg2': '37'}), ('/included/mixed_args/42/37/', 'inc-mixed-args', None, '', 'inc-mixed-args', views.empty_view, tuple(), {'arg2': '37'}), + ('/included/12/mixed_args/42/37/', 'inc-mixed-args', None, '', 'inc-mixed-args', views.empty_view, tuple(), {'arg2': '37'}), # Unnamed views should have None as the url_name. Regression data for #21157. ('/unnamed/normal/42/37/', None, None, '', 'urlpatterns_reverse.views.empty_view', views.empty_view, tuple(), {'arg1': '42', 'arg2': '37'}), @@ -49,6 +50,7 @@ resolve_test_data = ( # If you have no kwargs, you get an args list. ('/no_kwargs/42/37/', 'no-kwargs', None, '', 'no-kwargs', views.empty_view, ('42', '37'), {}), ('/included/no_kwargs/42/37/', 'inc-no-kwargs', None, '', 'inc-no-kwargs', views.empty_view, ('42', '37'), {}), + ('/included/12/no_kwargs/42/37/', 'inc-no-kwargs', None, '', 'inc-no-kwargs', views.empty_view, ('12', '42', '37'), {}), # Namespaces ('/test1/inner/42/37/', 'urlobject-view', 'testapp', 'test-ns1', 'test-ns1:urlobject-view', views.empty_view, tuple(), {'arg1': '42', 'arg2': '37'}),