Ticket #15900: 15900_normalize_namespace_prefixes_on_reverse.patch

File 15900_normalize_namespace_prefixes_on_reverse.patch, 3.7 KB (added by dmclain, 8 years ago)
  • django/core/urlresolvers.py

    diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py
    index eef8349..a35287e 100644
    a b class RegexURLResolver(object): 
    302302    def resolve500(self):
    303303        return self._resolve_special('500')
    304304
    305     def reverse(self, lookup_view, *args, **kwargs):   
    306         prefix = kwargs.pop('__DJANGO_REVERSE_PREFIX','')
     305    def reverse(self, lookup_view, *args, **kwargs):
    307306        if args and kwargs:
    308307            raise ValueError("Don't mix *args and **kwargs in call to reverse()!")
    309308        try:
    class RegexURLResolver(object): 
    311310        except (ImportError, AttributeError), e:
    312311            raise NoReverseMatch("Error importing '%s': %s." % (lookup_view, e))
    313312        possibilities = self.reverse_dict.getlist(lookup_view)
    314         prefix_norm, prefix_args = normalize(prefix)[0]
    315313        for possibility, pattern in possibilities:
    316314            for result, params in possibility:
    317315                if args:
    318                     if len(args) != len(params) + len(prefix_args):
     316                    if len(args) != len(params):
    319317                        continue
    320318                    unicode_args = [force_unicode(val) for val in args]
    321                     candidate =  (prefix_norm + result) % dict(zip(params, unicode_args))
     319                    candidate =  result % dict(zip(params, unicode_args))
    322320                else:
    323                     if set(kwargs.keys()) != (set(params)|set(prefix_args)):
     321                    if set(kwargs.keys()) != set(params):
    324322                        continue
    325323                    unicode_kwargs = dict([(k, force_unicode(v)) for (k, v) in kwargs.items()])
    326                     candidate = (prefix_norm + result) % unicode_kwargs
    327                 if re.search(u'^%s%s' % (prefix, pattern), candidate, re.UNICODE):
     324                    candidate = result % unicode_kwargs
     325                if re.search(u'^%s' % pattern, candidate, re.UNICODE):
    328326                    return candidate
    329327        # lookup_view can be URL label, or dotted path, or callable, Any of
    330328        # these can be passed in at the top, but callables are not friendly in
    def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None, current 
    388386                    raise NoReverseMatch("%s is not a registered namespace inside '%s'" % (key, ':'.join(resolved_path)))
    389387                else:
    390388                    raise NoReverseMatch("%s is not a registered namespace" % key)
    391     kwargs['__DJANGO_REVERSE_PREFIX'] = prefix
    392     return iri_to_uri(resolver.reverse(view, *args, **kwargs))
     389
     390    return iri_to_uri(u'%s%s' % (prefix, resolver.reverse(view,
     391            *args, **kwargs)))
    393392
    394393def clear_url_caches():
    395394    global _resolver_cache
  • tests/regressiontests/urlpatterns_reverse/namespace_urls.py

    diff --git a/tests/regressiontests/urlpatterns_reverse/namespace_urls.py b/tests/regressiontests/urlpatterns_reverse/namespace_urls.py
    index e3094e6..3d34049 100644
    a b urlpatterns = patterns('regressiontests.urlpatterns_reverse.views', 
    3737    (r'^default/', include(default_testobj.urls)),
    3838
    3939    (r'^other1/', include(otherobj1.urls)),
    40     (r'^other[246]/', include(otherobj2.urls)),
     40    (r'^other2/', include(otherobj2.urls)),
    4141
    42     (r'^ns-included[135]/', include('regressiontests.urlpatterns_reverse.included_namespace_urls', namespace='inc-ns1')),
     42    (r'^ns-included1/', include('regressiontests.urlpatterns_reverse.included_namespace_urls', namespace='inc-ns1')),
    4343    (r'^ns-included2/', include('regressiontests.urlpatterns_reverse.included_namespace_urls', namespace='inc-ns2')),
    4444
    4545    (r'^included/', include('regressiontests.urlpatterns_reverse.included_namespace_urls')),
Back to Top