Ticket #3530: urlresolvers_reverse_urlconf_func.diff

File urlresolvers_reverse_urlconf_func.diff, 1.0 KB (added by John Engdal, 17 years ago)

allows the user to specify a function in settings.py that returns the urlconf to be used in reverse.

  • django/core/urlresolvers.py

     
    291291def resolve(path, urlconf=None):
    292292    return get_resolver(urlconf).resolve(path)
    293293
     294def urlconf_func():
     295    from django.conf import settings
     296    if not hasattr(settings, 'DYNAMIC_URLCONF_FUNCTION'):
     297        return None
     298    try:
     299        mod_name, func_name = get_mod_func(getattr(settings, 'DYNAMIC_URLCONF_FUNCTION'))
     300        return getattr(__import__(mod_name, {}, {}, ['']), func_name)
     301    except Exception, e:
     302        raise ImproperlyConfigured, "Error while importing DYNAMIC_URLCONF_FUNCTION from settings.py: %s" % (e)
     303urlconf_func = urlconf_func()
     304
    294305def reverse(viewname, urlconf=None, args=None, kwargs=None):
     306    if urlconf_func and urlconf is None:
     307        urlconf = urlconf_func()
    295308    args = args or []
    296309    kwargs = kwargs or {}
    297310    return iri_to_uri(u'/' + get_resolver(urlconf).reverse(viewname, *args, **kwargs))
    298 
Back to Top