Ticket #11559: ticket_11559.diff
File ticket_11559.diff, 3.9 KB (added by , 13 years ago) |
---|
-
django/core/urlresolvers.py
20 20 from django.utils.regex_helper import normalize 21 21 22 22 _resolver_cache = {} # Maps URLconf modules to RegexURLResolver instances. 23 _ns_resolver_cache = {} # Maps namespaces to RegexURLResolver instances. 23 24 _callable_cache = {} # Maps view and url pattern names to their view functions. 24 25 25 26 # SCRIPT_NAME prefixes for each thread are stored here. If there's no entry for … … 106 107 return RegexURLResolver(r'^/', urlconf) 107 108 get_resolver = memoize(get_resolver, _resolver_cache, 1) 108 109 110 def get_ns_resolver(ns_pattern, resolver): 111 # Build a namespaced resolver for the given parent urlconf pattern. 112 # This makes it possible to have captured parameters in the parent 113 # urlconf pattern. 114 ns_resolver = RegexURLResolver(ns_pattern, resolver.url_patterns) 115 return RegexURLResolver(r'^/', [ns_resolver]) 116 get_ns_resolver = memoize(get_ns_resolver, _ns_resolver_cache, 2) 117 109 118 def get_mod_func(callback): 110 119 # Converts 'django.views.news.stories.story_detail' to 111 120 # ['django.views.news.stories', 'story_detail'] … … 367 376 path = parts[1:] 368 377 369 378 resolved_path = [] 379 ns_pattern = '' 370 380 while path: 371 381 ns = path.pop() 372 382 … … 387 397 try: 388 398 extra, resolver = resolver.namespace_dict[ns] 389 399 resolved_path.append(ns) 390 prefix = prefix+ extra400 ns_pattern = ns_pattern + extra 391 401 except KeyError, key: 392 402 if resolved_path: 393 403 raise NoReverseMatch("%s is not a registered namespace inside '%s'" % (key, ':'.join(resolved_path))) 394 404 else: 395 405 raise NoReverseMatch("%s is not a registered namespace" % key) 406 if ns_pattern: 407 resolver = get_ns_resolver(ns_pattern, resolver) 396 408 397 409 return iri_to_uri(u'%s%s' % (prefix, resolver.reverse(view, 398 410 *args, **kwargs))) -
tests/regressiontests/urlpatterns_reverse/namespace_urls.py
44 44 45 45 (r'^included/', include('regressiontests.urlpatterns_reverse.included_namespace_urls')), 46 46 47 (r'^ns-outer/(?P<outer>\d+)/', include('regressiontests.urlpatterns_reverse.included_namespace_urls', namespace='inc-outer')), 48 47 49 ) -
tests/regressiontests/urlpatterns_reverse/tests.py
327 327 self.assertEqual('/ns-included1/normal/', reverse('inc-ns1:inc-normal-view')) 328 328 self.assertEqual('/ns-included1/normal/37/42/', reverse('inc-ns1:inc-normal-view', args=[37,42])) 329 329 self.assertEqual('/ns-included1/normal/42/37/', reverse('inc-ns1:inc-normal-view', kwargs={'arg1':42, 'arg2':37})) 330 331 def test_namespace_pattern_with_variable_prefix(self): 332 "When using a include with namespaces when there is a regex variable in front of it" 333 self.assertEqual('/ns-outer/42/normal/', reverse('inc-outer:inc-normal-view', kwargs={'outer':42})) 334 self.assertEqual('/ns-outer/42/normal/', reverse('inc-outer:inc-normal-view', args=[42])) 335 self.assertEqual('/ns-outer/42/normal/37/4/', reverse('inc-outer:inc-normal-view', kwargs={'outer':42, 'arg1': 37, 'arg2': 4})) 336 self.assertEqual('/ns-outer/42/normal/37/4/', reverse('inc-outer:inc-normal-view', args=[42, 37, 4])) 330 337 331 338 def test_multiple_namespace_pattern(self): 332 339 "Namespaces can be embedded"