Ticket #682: fully-decoupled-urlconfs-v2.diff

File fully-decoupled-urlconfs-v2.diff, 1.2 KB (added by sa@…, 19 years ago)

Cleaner impl that also works with generic views.

  • django/conf/urls/defaults.py

     
     1import sys
    12from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
    23
    3 __all__ = ['handler404', 'handler500', 'include', 'patterns']
     4__all__ = ['handler404', 'handler500', 'include', 'patterns', 'prefix']
    45
    56handler404 = 'django.views.defaults.page_not_found'
    67handler500 = 'django.views.defaults.server_error'
    78
    89include = lambda urlconf_module: [urlconf_module]
    910
     11def prefix(postfix):
     12    """
     13    Figures out and adds a prefix to the given postfix by
     14    examining the module name of the calling URLconf.
     15   
     16    if the given postfix is:
     17        'views.polls.vote'
     18    and this function is called from:
     19        'myproject.apps.polls.urls'
     20    the return value will be:
     21        'myproject.apps.polls.views.polls.vote'
     22    """
     23    f = sys._getframe(1)
     24    module = f.f_globals["__name__"]
     25    segs = module.split(".")
     26    segs = segs[:3]
     27    segs.append(postfix)
     28    return ".".join(segs)
     29
    1030def patterns(prefix, *tuples):
    1131    pattern_list = []
    1232    for t in tuples:
Back to Top