@contextlib.contextmanager
def replace_urls(patterns, extra=None):
 old_urlconf = get_urlconf(default=django.conf.settings.ROOT_URLCONF)
 urlconf_module_name = ('replace_urls_%s' % uuid.uuid4())
 module = types.ModuleType(urlconf_module_name)
 module.urlpatterns = patterns
 module.__dict__.update((extra or ()))
 sys.modules[urlconf_module_name] = module
 set_urlconf(urlconf_module_name)
 clear_url_caches()
 with override_settings(ROOT_URLCONF=urlconf_module_name):
  (yield)
 set_urlconf(old_urlconf)
 clear_url_caches()
 sys.modules.pop(urlconf_module_name)

@ contextlib . contextmanager def replace_urls ( patterns , extra = None ) : old_urlconf = get_urlconf ( default = django . conf . settings . ROOT_URLCONF ) urlconf_module_name = ( 'replace_urls_%s' % uuid . uuid4 ( ) ) module = types . ModuleType ( urlconf_module_name ) module . urlpatterns = patterns module . __dict__ . update ( ( extra or ( ) ) ) sys . modules [ urlconf_module_name ] = module set_urlconf ( urlconf_module_name ) clear_url_caches ( ) with override_settings ( ROOT_URLCONF = urlconf_module_name ) : ( yield ) set_urlconf ( old_urlconf ) clear_url_caches ( ) sys . modules . pop ( urlconf_module_name )
context manager to replace the root urlconf with a list of urlpatterns in-memory .