Ticket #5034: set_urlconf.2.diff
File set_urlconf.2.diff, 8.1 KB (added by , 15 years ago) |
---|
-
django/core/urlresolvers.py
32 32 # be empty. 33 33 _prefixes = {} 34 34 35 # Overridden URLconfs for each thread are stored here. 36 _urlconfs = {} 37 35 38 class Resolver404(Http404): 36 39 pass 37 40 … … 300 303 "arguments '%s' not found." % (lookup_view_s, args, kwargs)) 301 304 302 305 def resolve(path, urlconf=None): 306 if urlconf is None: 307 urlconf = get_urlconf() 303 308 return get_resolver(urlconf).resolve(path) 304 309 305 310 def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None, current_app=None): 311 if urlconf is None: 312 urlconf = get_urlconf() 306 313 resolver = get_resolver(urlconf) 307 314 args = args or [] 308 315 kwargs = kwargs or {} … … 371 378 """ 372 379 return _prefixes.get(currentThread(), u'/') 373 380 381 def set_urlconf(urlconf_name): 382 """ 383 Sets the URLconf for the current thread (overriding the default one in 384 settings). Set to None to revert back to the default. 385 """ 386 thread = currentThread() 387 if urlconf_name: 388 _urlconfs[thread] = urlconf_name 389 else: 390 # This is a lot faster than wrapping in a try/except. 391 if thread in _urlconfs: 392 del _urlconfs[thread] 393 394 def get_urlconf(default=None): 395 """ 396 Returns the root URLconf to use for the current thread if it has been 397 changed from the default one. 398 """ 399 thread = currentThread() 400 # It's faster to check first than to do _urlconfs.get(thread) 401 if thread in _urlconfs: 402 return _urlconfs[thread] 403 return default 404 No newline at end of file -
django/core/handlers/base.py
67 67 "Returns an HttpResponse object for the given HttpRequest" 68 68 from django.core import exceptions, urlresolvers 69 69 from django.conf import settings 70 70 71 # Reset the urlconf for this thread. 72 urlresolvers.set_urlconf(None) 73 71 74 # Apply request middleware 72 75 for middleware_method in self._request_middleware: 73 76 response = middleware_method(request) 74 77 if response: 75 78 return response 76 79 77 # Get urlconf from request object, if available. Otherwise use default. 78 urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF) 80 # Get overridden urlconf for this thread, if available. Otherwise use 81 # default. 82 urlconf = urlresolvers.get_urlconf() 83 if urlconf is None: 84 # Get urlconf from request object, if available (deprecated). 85 if hasattr(request, 'urlconf'): 86 import warnings 87 warnings.warn("The 'request.urlconf' attribute is deprecated. " 88 "Use the 'django.core.urlresolvers::set_urlconf'" 89 " method instead.", DeprecationWarning) 90 urlconf = request.urlconf 91 urlresolvers.set_urlconf(urlconf) 92 else: 93 urlconf = settings.ROOT_URLCONF 79 94 80 95 resolver = urlresolvers.RegexURLResolver(r'^/', urlconf) 81 96 try: … … 132 147 exc_info = sys.exc_info() 133 148 receivers = signals.got_request_exception.send(sender=self.__class__, request=request) 134 149 return self.handle_uncaught_exception(request, resolver, exc_info) 150 finally: 151 # Reset the urlconf for this thread. 152 urlresolvers.set_urlconf(None) 135 153 136 154 def handle_uncaught_exception(self, request, resolver, exc_info): 137 155 """ -
tests/regressiontests/urlpatterns_reverse/tests.py
14 14 ImproperlyConfigured: The included urlconf regressiontests.urlpatterns_reverse.no_urls doesn't have any patterns in it 15 15 """} 16 16 17 import re 17 18 import unittest 18 19 20 from django.conf import settings 19 21 from django.core.urlresolvers import reverse, resolve, NoReverseMatch, Resolver404 20 22 from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect 21 23 from django.shortcuts import redirect 22 24 from django.test import TestCase 23 25 26 import urlconf_outer 27 import urlconf_inner 28 import middleware 29 24 30 test_data = ( 25 31 ('places', '/places/3/', [3], {}), 26 32 ('places', '/places/3/', ['3'], {}), … … 239 245 self.assertEquals('/other1/inner/37/42/', reverse('nodefault:urlobject-view', args=[37,42], current_app='other-ns1')) 240 246 self.assertEquals('/other1/inner/42/37/', reverse('nodefault:urlobject-view', kwargs={'arg1':42, 'arg2':37}, current_app='other-ns1')) 241 247 248 249 class SetUrlConf(TestCase): 250 def setUp(self): 251 self.root_urlconf = settings.ROOT_URLCONF 252 self.middleware_classes = settings.MIDDLEWARE_CLASSES 253 settings.ROOT_URLCONF = urlconf_outer.__name__ 254 255 def tearDown(self): 256 settings.ROOT_URLCONF = self.root_urlconf 257 settings.MIDDLEWARE_CLASSES = self.middleware_classes 258 259 def test_handler(self): 260 response = self.client.get('/test/me/') 261 self.assertEqual(response.status_code, 200) 262 self.assertEqual(response.content, 'outer:/test/me/,' 263 'inner:/inner_urlconf/second_test/') 264 response = self.client.get('/inner_urlconf/second_test/') 265 self.assertEqual(response.status_code, 200) 266 response = self.client.get('/second_test/') 267 self.assertEqual(response.status_code, 404) 268 269 def test_handler_overridden(self): 270 settings.MIDDLEWARE_CLASSES += ( 271 '%s.ChangeUrlconfMiddleware' % middleware.__name__, 272 ) 273 response = self.client.get('/test/me/') 274 self.assertEqual(response.status_code, 404) 275 response = self.client.get('/inner_urlconf/second_test/') 276 self.assertEqual(response.status_code, 404) 277 response = self.client.get('/second_test/') 278 self.assertEqual(response.status_code, 200) 279 self.assertEqual(response.content, 'outer:,inner:/second_test/') 280 No newline at end of file -
docs/topics/http/urls.txt
39 39 algorithm the system follows to determine which Python code to execute: 40 40 41 41 1. Django determines the root URLconf module to use. Ordinarily, 42 this is the value of the ``ROOT_URLCONF`` setting, but if the incoming 43 ``HttpRequest`` object has an attribute called ``urlconf``, its value 44 will be used in place of the ``ROOT_URLCONF`` setting. 42 this is the value of the ``ROOT_URLCONF`` setting, this can be overridden 43 per request by calling the ``set_urlconf`` method found in 44 ``django.core.urlresolvers`` (passing in the value to be used in place of 45 the ``ROOT_URLCONF`` setting). 45 46 46 47 2. Django loads that Python module and looks for the variable 47 48 ``urlpatterns``. This should be a Python list, in the format returned by -
docs/ref/settings.txt
804 804 805 805 Default: Not defined 806 806 807 A string representing the full Python import path to your root URLconf. For example:808 ``"mydjangoapps.urls"``. Can be overridden on a per-request basis by809 setting the attribute ``urlconf`` on the incoming ``HttpRequest`` 810 object. See:ref:`how-django-processes-a-request` for details.807 A string representing the full Python import path to your root URLconf. For 808 example: ``"mydjangoapps.urls"``. Can be overridden on a per-request basis by 809 using the ``set_urlconf`` method in ``django.core.urlresolvers``. See 810 :ref:`how-django-processes-a-request` for details. 811 811 812 812 .. setting:: SECRET_KEY 813 813