Ticket #1192: url_resolver2.diff
File url_resolver2.diff, 4.5 KB (added by , 19 years ago) |
---|
-
django/conf/global_settings.py
132 132 # Whether to prepend the "www." subdomain to URLs that don't have it. 133 133 PREPEND_WWW = False 134 134 135 # class that will resolve the url 136 URL_RESOLVER = 'django.core.urlresolvers.URLResolver' 137 135 138 # List of compiled regular expression objects representing User-Agent strings 136 139 # that are not allowed to visit any page, systemwide. Use this for bad 137 140 # robots/crawlers. Here are a few examples: -
django/core/urlresolvers.py
9 9 10 10 from django.http import Http404 11 11 from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist 12 import re 12 import re, types 13 13 14 14 class Resolver404(Http404): 15 15 pass … … 59 59 raise ViewDoesNotExist, "Tried %s in module %s. Error was: %s" % (func_name, mod_name, str(e)) 60 60 61 61 class RegexURLResolver(object): 62 def __init__(self, regex, url conf_name):62 def __init__(self, regex, url_patterns): 63 63 # regex is a string representing a regular expression. 64 # urlconf_name is a string representing the module containing urlconfs. 64 # urlconf_name is a string representing the module containing urlconfs, or 65 # a tupple with parsed urlpatterns 65 66 self.regex = re.compile(regex) 66 self.urlconf_name = urlconf_name 67 if isinstance(url_patterns, types.StringTypes): 68 self.urlconf_name = url_patterns 69 else: 70 self._url_patterns = url_patterns 71 67 72 68 73 def resolve(self, path): 69 74 tried = [] 70 75 match = self.regex.search(path) 71 76 if match: 72 77 new_path = path[match.end():] 73 for pattern in self.url conf_module.urlpatterns:78 for pattern in self.url_patterns: 74 79 try: 75 80 sub_match = pattern.resolve(new_path) 76 81 except Resolver404, e: … … 87 92 except AttributeError: 88 93 try: 89 94 self._urlconf_module = __import__(self.urlconf_name, '', '', ['']) 90 except ValueError, e:95 except (ValueError, ImportError), e: 91 96 # Invalid urlconf_name, such as "foo.bar." (note trailing period) 92 97 raise ImproperlyConfigured, "Error while importing URLconf %r: %s" % (self.urlconf_name, e) 93 98 return self._urlconf_module 94 99 urlconf_module = property(_get_urlconf_module) 95 100 96 101 def _get_url_patterns(self): 97 return self.urlconf_module.urlpatterns 102 try: 103 return self._url_patterns 104 except AttributeError: 105 self._url_patterns = self.urlconf_module.urlpatterns 106 return self._url_patterns 98 107 url_patterns = property(_get_url_patterns) 99 108 100 109 def _resolve_special(self, view_type): … … 110 119 111 120 def resolve500(self): 112 121 return self._resolve_special('500') 122 123 class URLResolver(RegexURLResolver): 124 def __init__(self, request, urlconf_name): 125 RegexURLResolver.__init__(self, r'^/', urlconf_name) -
django/core/handlers/base.py
59 59 if response: 60 60 return response 61 61 62 resolver = urlresolvers.RegexURLResolver(r'^/', settings.ROOT_URLCONF)63 62 try: 63 dot = settings.URL_RESOLVER.rindex('.') 64 except ValueError: 65 raise exceptions.ImproperlyConfigured, '%s isn\'t a resolver module' % URL_RESOLVER 66 ur_module, ur_classname = URL_RESOLVER[:dot], URL_RESOLVER[dot+1:] 67 try: 68 mod = __import__(ur_module, '', '', ['']) 69 except ImportError, e: 70 raise exceptions.ImproperlyConfigured, 'Error importing urlresolver module %s: "%s"' % (ur_module, e) 71 try: 72 ur_class = getattr(mod, ur_classname) 73 except AttributeError: 74 raise exceptions.ImproperlyConfigured, 'Urlresolver module "%s" does not define a "%s" class' % (mw_module, mw_classname) 75 76 resolver = ur_class(request, settings.ROOT_URLCONF) 77 78 try: 64 79 callback, callback_args, callback_kwargs = resolver.resolve(path) 65 80 66 81 # Apply view middleware