Ticket #682: fully-decoupled-urlconfs.diff

File fully-decoupled-urlconfs.diff, 1.2 KB (added by sa@…, 10 years ago)
  • django/conf/urls/defaults.py

     
     1import sys
    12from django.core.urlresolvers import RegexURLPattern, RegexURLResolver
    23
    34__all__ = ['handler404', 'handler500', 'include', 'patterns']
     
    78
    89include = lambda urlconf_module: [urlconf_module]
    910
     11
     12def _get_prefix():
     13    """
     14    Figures out the prefix for the calling URLconf module.
     15
     16    If the calling URLconf is at:
     17        myproject.apps.polls.urls
     18    the returned prefix will be:
     19        myproject.apps.polls.views.polls
     20    """
     21    f = sys._getframe(2)
     22    module = f.f_globals["__name__"]
     23    segs = module.split(".")
     24    # get rid of the 'urls' segment
     25    segs.pop()
     26    # now the last segment is the apps name
     27    app_name = segs[-1]
     28    # inject the 'views' module
     29    segs.append("views")
     30    # add the apps name to the end
     31    segs.append(app_name)
     32    # return the generated prefix
     33    return ".".join(segs)
     34
    1035def patterns(prefix, *tuples):
     36    if prefix is None:
     37        prefix = _get_prefix()
    1138    pattern_list = []
    1239    for t in tuples:
    1340        if type(t[1]) == list:
Back to Top