Ticket #11559: urlresolvers.reverse.hack.diff

File urlresolvers.reverse.hack.diff, 1.9 KB (added by cwb, 15 years ago)

Maybe SVN diff against trunk with .diff extension then.. (Patch not tested on trunk)

  • django/core/urlresolvers.py

     
    346346                else:
    347347                    raise NoReverseMatch("%s is not a registered namespace" % key)
    348348
     349    # This is a hack to get namespaced URLs to reverse when the base
     350    # has named parameters (may work for positional, but not tested at all).
     351    pattern = prefix
     352    possibility = normalize(pattern) # The magical regex reverse function!
     353    # Below copied from _populate above and slightly altered
     354    for result, params in possibility:
     355        copy_args = [v for v in args]
     356        copy_kwargs = kwargs.copy()
     357        if args:
     358            if len(args) < len(params):
     359                continue
     360            # Pop arguments to ensure the right number are passed on to the
     361            # resolver.
     362            unicode_args = [force_unicode(copy_args.pop(0)) for v in params]
     363            candidate =  result % dict(zip(params, unicode_args))
     364        else:
     365            if not set(params).issubset(set(kwargs.keys())):
     366                continue
     367            # Pop arguments to ensure the right number are passed on to the
     368            # resolver.
     369            unicode_kwargs = dict([(k, force_unicode(copy_kwargs.pop(k))) for k in params])
     370            candidate = result % unicode_kwargs
     371        if re.search(u'^%s' % pattern, candidate, re.UNICODE):
     372            prefix = candidate
     373        else:
     374            raise NoReverseMatch(
     375                "Reverse for '%s' with arguments '%s' and keyword arguments\
     376                '%s' not found." % (pattern, args, kwargs))
     377
    349378    return iri_to_uri(u'%s%s' % (prefix, resolver.reverse(view,
    350             *args, **kwargs)))
     379            *copy_args, **copy_kwargs)))
    351380
    352381def clear_url_caches():
    353382    global _resolver_cache
Back to Top