Ticket #1192: django_urlresolve_patch.diff
File django_urlresolve_patch.diff, 8.6 KB (added by , 19 years ago) |
---|
-
conf/global_settings.py
130 130 # Whether to prepend the "www." subdomain to URLs that don't have it. 131 131 PREPEND_WWW = False 132 132 133 # class that will resolve the url 134 URL_RESOLVER = 'django.core.urlresolvers.URLResolver' 135 133 136 # List of compiled regular expression objects representing User-Agent strings 134 137 # that are not allowed to visit any page, systemwide. Use this for bad 135 138 # robots/crawlers. Here are a few examples: -
core/urlresolvers.py
8 8 """ 9 9 10 10 from django.core.exceptions import Http404, ImproperlyConfigured, ViewDoesNotExist 11 import re 11 import re, types 12 12 13 13 class Resolver404(Http404): 14 14 pass … … 58 58 raise ViewDoesNotExist, "Tried %s in module %s. Error was: %s" % (func_name, mod_name, str(e)) 59 59 60 60 class RegexURLResolver(object): 61 def __init__(self, regex, url conf_name):61 def __init__(self, regex, url_patterns): 62 62 # regex is a string representing a regular expression. 63 # urlconf_name is a string representing the module containing urlconfs. 63 # urlconf_name is a string representing the module containing urlconfs, or 64 # a tupple with parsed urlpatterns 64 65 self.regex = re.compile(regex) 65 self.urlconf_name = urlconf_name 66 if isinstance(url_patterns, types.StringTypes): 67 self.urlconf_name = url_patterns 68 else: 69 self._url_patterns = url_patterns 70 66 71 67 72 def resolve(self, path): 68 73 tried = [] 69 74 match = self.regex.search(path) 70 75 if match: 71 76 new_path = path[match.end():] 72 for pattern in self.url conf_module.urlpatterns:77 for pattern in self.url_patterns: 73 78 try: 74 79 sub_match = pattern.resolve(new_path) 75 80 except Resolver404, e: … … 86 91 except AttributeError: 87 92 try: 88 93 self._urlconf_module = __import__(self.urlconf_name, '', '', ['']) 89 except ValueError, e:94 except (ValueError, ImportError), e: 90 95 # Invalid urlconf_name, such as "foo.bar." (note trailing period) 91 96 raise ImproperlyConfigured, "Error while importing URLconf %r: %s" % (self.urlconf_name, e) 92 97 return self._urlconf_module 93 98 urlconf_module = property(_get_urlconf_module) 94 99 95 100 def _get_url_patterns(self): 96 return self.urlconf_module.urlpatterns 101 try: 102 return self._url_patterns 103 except AttributeError: 104 self._url_patterns = self.urlconf_module.urlpatterns 105 return self._url_patterns 97 106 url_patterns = property(_get_url_patterns) 98 107 99 108 def _resolve_special(self, view_type): … … 109 118 110 119 def resolve500(self): 111 120 return self._resolve_special('500') 121 122 class URLResolver(RegexURLResolver): 123 def __init__(self, request, urlconf_name): 124 RegexURLResolver.__init__(self, r'^/', urlconf_name) 125 126 class HostURLResolver(RegexURLResolver): 127 """ 128 The HostURLResolver resolves paths like URLResolver, but only on matching hostnames. 129 Note: The urlpatterns have a different layout. 130 131 urlpatterns = ( 132 (r'^localhost$', patterns('', 133 (r'^admin/', include('django.contrib.admin.urls.admin')), 134 ), 135 ), 136 # default view, matches always, even when no HTTP_HOST is sent (HTTP 1.0) 137 (r'', patterns('', 138 ... 139 ), 140 ), 141 ) 142 """ 143 144 def __init__(self, request, urlconf_name): 145 self.request = request 146 self.urlconf_name = urlconf_name 147 148 def resolve(self, path): 149 host = self.request.META.get('HTTP_HOST') 150 # if IGNORE_HOST_PORT is true, we remove the port from 151 # HTTP_POST and match only the hostname 152 try: 153 if self.urlconf_module.IGNORE_HOST_PORT: 154 host = host[:host.rfind(":")] 155 except: pass 156 tried = [] 157 for thost, patterns in self.urlconf_module.urlpatterns: 158 # HTTP 1.0 doesn't send a HTTP_HOST so we can only match the default 159 # host which is defined as a empty string 160 if thost != "": 161 if host == None: 162 continue 163 if not re.match(thost, host): 164 tried.append((thost, None)) 165 continue 166 try: 167 r = RegexURLResolver(r'^/', patterns) 168 return r.resolve(path) 169 except Resolver404, e: 170 tried.append((host, e.args[0]['tried'])) 171 172 raise Resolver404, {'tried': tried, 'host':host} 173 urlconf_module = property(RegexURLResolver._get_urlconf_module) -
core/handlers/base.py
51 51 "Returns an HttpResponse object for the given HttpRequest" 52 52 from django.core import exceptions, urlresolvers 53 53 from django.core.mail import mail_admins 54 from django.conf.settings import DEBUG, INTERNAL_IPS, ROOT_URLCONF 54 from django.conf.settings import DEBUG, INTERNAL_IPS, ROOT_URLCONF, URL_RESOLVER 55 55 56 56 # Apply request middleware 57 57 for middleware_method in self._request_middleware: 58 58 response = middleware_method(request) 59 59 if response: 60 60 return response 61 62 resolver = urlresolvers.RegexURLResolver(r'^/', ROOT_URLCONF)63 61 try: 62 dot = URL_RESOLVER.rindex('.') 63 except ValueError: 64 raise exceptions.ImproperlyConfigured, '%s isn\'t a resolver module' % URL_RESOLVER 65 ur_module, ur_classname = URL_RESOLVER[:dot], URL_RESOLVER[dot+1:] 66 try: 67 mod = __import__(ur_module, '', '', ['']) 68 except ImportError, e: 69 raise exceptions.ImproperlyConfigured, 'Error importing urlresolver module %s: "%s"' % (ur_module, e) 70 try: 71 ur_class = getattr(mod, ur_classname) 72 except AttributeError: 73 raise exceptions.ImproperlyConfigured, 'Urlresolver module "%s" does not define a "%s" class' % (mw_module, mw_classname) 74 75 resolver = ur_class(request, ROOT_URLCONF) 76 77 try: 64 78 callback, callback_args, callback_kwargs = resolver.resolve(path) 65 79 66 80 # Apply view middleware -
views/debug.py
135 135 if not tried: 136 136 # tried exists but is an empty list. The URLconf must've been empty. 137 137 return empty_urlconf(request) 138 138 try: 139 host = exception.args[0]['host'] 140 except: 141 host = False 142 139 143 t = Template(TECHNICAL_404_TEMPLATE) 140 144 c = Context({ 141 145 'root_urlconf': settings.ROOT_URLCONF, 142 146 'urlpatterns': tried, 147 'host': host, 143 148 'reason': str(exception), 144 149 'request': request, 145 150 'request_protocol': os.environ.get("HTTPS") == "on" and "https" or "http", … … 547 552 {% if urlpatterns %} 548 553 <p> 549 554 Using the URLconf defined in <code>{{ settings.ROOT_URLCONF }}</code>, 555 {% if host %} 556 Django tried these URL patterns with hostname <code>{{ host|escape }}</code>, in this order: 557 {% else %} 550 558 Django tried these URL patterns, in this order: 559 {% endif %} 551 560 </p> 552 561 <ol> 553 {% for pattern in urlpatterns %} 554 <li>{{ pattern|escape }}</li> 555 {% endfor %} 562 {% if host %} 563 {% for pattern in urlpatterns %} 564 <li>{{ pattern.0|escape }}<br/> 565 {% if pattern.1 %} 566 <ol> 567 {% for match in pattern.1 %} 568 <li>{{ match|escape}}</li> 569 {% endfor %} 570 </ol> 571 {% else %} 572 Hostname didn't match, or empty url pattern 573 {% endif %} 574 </li> 575 {% endfor %} 576 {% else %} 577 {% for pattern in urlpatterns %} 578 <li>{{ pattern|escape }}</li> 579 {% endfor %} 580 {% endif %} 556 581 </ol> 557 582 <p>The current URL, <code>{{ request.path }}</code>, didn't match any of these.</p> 558 583 {% else %}