2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
This module converts requested URLs to callback view functions.
|
|
|
|
|
|
|
|
RegexURLResolver is the main class here. Its resolve() method takes a URL (as
|
|
|
|
a string) and returns a tuple in this format:
|
|
|
|
|
2005-11-28 06:08:51 +08:00
|
|
|
(view_function, function_args, function_kwargs)
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
|
2008-07-21 15:57:10 +08:00
|
|
|
import re
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.http import Http404
|
2009-11-16 09:58:00 +08:00
|
|
|
from django.conf import settings
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
|
2008-09-27 14:14:11 +08:00
|
|
|
from django.utils.datastructures import MultiValueDict
|
2007-07-08 19:22:53 +08:00
|
|
|
from django.utils.encoding import iri_to_uri, force_unicode, smart_str
|
2007-06-23 13:40:28 +08:00
|
|
|
from django.utils.functional import memoize
|
2009-03-19 00:55:59 +08:00
|
|
|
from django.utils.importlib import import_module
|
2008-08-31 19:11:20 +08:00
|
|
|
from django.utils.regex_helper import normalize
|
2008-07-21 15:57:10 +08:00
|
|
|
from django.utils.thread_support import currentThread
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-06-23 14:02:17 +08:00
|
|
|
try:
|
|
|
|
reversed
|
|
|
|
except NameError:
|
|
|
|
from django.utils.itercompat import reversed # Python 2.3 fallback
|
2008-08-31 19:11:20 +08:00
|
|
|
from sets import Set as set
|
2007-06-23 14:02:17 +08:00
|
|
|
|
2009-04-01 00:07:07 +08:00
|
|
|
_resolver_cache = {} # Maps URLconf modules to RegexURLResolver instances.
|
2007-06-23 13:40:28 +08:00
|
|
|
_callable_cache = {} # Maps view and url pattern names to their view functions.
|
|
|
|
|
2008-07-21 15:57:10 +08:00
|
|
|
# SCRIPT_NAME prefixes for each thread are stored here. If there's no entry for
|
|
|
|
# the current thread (which is the only one we ever access), it is assumed to
|
|
|
|
# be empty.
|
|
|
|
_prefixes = {}
|
|
|
|
|
2009-11-16 09:58:00 +08:00
|
|
|
# Overridden URLconfs for each thread are stored here.
|
|
|
|
_urlconfs = {}
|
|
|
|
|
2005-08-06 06:22:41 +08:00
|
|
|
class Resolver404(Http404):
|
|
|
|
pass
|
|
|
|
|
2006-05-16 12:05:55 +08:00
|
|
|
class NoReverseMatch(Exception):
|
2006-10-10 07:24:48 +08:00
|
|
|
# Don't make this raise an error when used in a template.
|
2007-02-26 06:31:31 +08:00
|
|
|
silent_variable_failure = True
|
2006-05-16 12:05:55 +08:00
|
|
|
|
2007-06-25 12:34:34 +08:00
|
|
|
def get_callable(lookup_view, can_fail=False):
|
|
|
|
"""
|
|
|
|
Convert a string version of a function name to the callable object.
|
|
|
|
|
|
|
|
If the lookup_view is not an import path, it is assumed to be a URL pattern
|
|
|
|
label and the original string is returned.
|
|
|
|
|
|
|
|
If can_fail is True, lookup_view might be a URL pattern label, so errors
|
|
|
|
during the import fail and the string is returned.
|
|
|
|
"""
|
2007-07-08 08:39:32 +08:00
|
|
|
if not callable(lookup_view):
|
|
|
|
try:
|
|
|
|
# Bail early for non-ASCII strings (they can't be functions).
|
|
|
|
lookup_view = lookup_view.encode('ascii')
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
mod_name, func_name = get_mod_func(lookup_view)
|
2007-07-08 08:39:32 +08:00
|
|
|
if func_name != '':
|
2009-03-19 00:55:59 +08:00
|
|
|
lookup_view = getattr(import_module(mod_name), func_name)
|
2008-08-29 03:05:14 +08:00
|
|
|
if not callable(lookup_view):
|
|
|
|
raise AttributeError("'%s.%s' is not a callable." % (mod_name, func_name))
|
2007-07-08 08:39:32 +08:00
|
|
|
except (ImportError, AttributeError):
|
|
|
|
if not can_fail:
|
|
|
|
raise
|
|
|
|
except UnicodeEncodeError:
|
|
|
|
pass
|
2007-06-23 13:40:28 +08:00
|
|
|
return lookup_view
|
2007-07-08 08:39:32 +08:00
|
|
|
get_callable = memoize(get_callable, _callable_cache, 1)
|
2007-06-23 13:40:28 +08:00
|
|
|
|
|
|
|
def get_resolver(urlconf):
|
|
|
|
if urlconf is None:
|
|
|
|
from django.conf import settings
|
|
|
|
urlconf = settings.ROOT_URLCONF
|
|
|
|
return RegexURLResolver(r'^/', urlconf)
|
2007-07-08 08:39:32 +08:00
|
|
|
get_resolver = memoize(get_resolver, _resolver_cache, 1)
|
2007-06-23 13:40:28 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def get_mod_func(callback):
|
|
|
|
# Converts 'django.views.news.stories.story_detail' to
|
|
|
|
# ['django.views.news.stories', 'story_detail']
|
2006-11-07 13:05:16 +08:00
|
|
|
try:
|
|
|
|
dot = callback.rindex('.')
|
|
|
|
except ValueError:
|
|
|
|
return callback, ''
|
2005-07-13 09:25:57 +08:00
|
|
|
return callback[:dot], callback[dot+1:]
|
|
|
|
|
2006-06-08 13:00:13 +08:00
|
|
|
class RegexURLPattern(object):
|
2007-04-01 15:25:20 +08:00
|
|
|
def __init__(self, regex, callback, default_args=None, name=None):
|
2005-08-06 04:50:19 +08:00
|
|
|
# regex is a string representing a regular expression.
|
2006-08-11 15:01:29 +08:00
|
|
|
# callback is either a string like 'foo.views.news.stories.story_detail'
|
|
|
|
# which represents the path to a module and a view function name, or a
|
|
|
|
# callable object (view).
|
2007-07-08 02:24:27 +08:00
|
|
|
self.regex = re.compile(regex, re.UNICODE)
|
2006-08-11 15:01:29 +08:00
|
|
|
if callable(callback):
|
|
|
|
self._callback = callback
|
|
|
|
else:
|
|
|
|
self._callback = None
|
|
|
|
self._callback_str = callback
|
2005-07-13 09:25:57 +08:00
|
|
|
self.default_args = default_args or {}
|
2007-04-01 15:25:20 +08:00
|
|
|
self.name = name
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-07-13 17:14:51 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return '<%s %s %s>' % (self.__class__.__name__, self.name, self.regex.pattern)
|
|
|
|
|
2007-04-26 19:17:19 +08:00
|
|
|
def add_prefix(self, prefix):
|
|
|
|
"""
|
|
|
|
Adds the prefix string to a string-based callback.
|
|
|
|
"""
|
|
|
|
if not prefix or not hasattr(self, '_callback_str'):
|
|
|
|
return
|
|
|
|
self._callback_str = prefix + '.' + self._callback_str
|
|
|
|
|
2005-08-06 04:50:19 +08:00
|
|
|
def resolve(self, path):
|
2005-07-13 09:25:57 +08:00
|
|
|
match = self.regex.search(path)
|
|
|
|
if match:
|
2005-11-28 06:08:51 +08:00
|
|
|
# If there are any named groups, use those as kwargs, ignoring
|
|
|
|
# non-named groups. Otherwise, pass all non-named arguments as
|
|
|
|
# positional arguments.
|
|
|
|
kwargs = match.groupdict()
|
|
|
|
if kwargs:
|
|
|
|
args = ()
|
2007-02-26 06:31:31 +08:00
|
|
|
else:
|
2005-11-28 06:08:51 +08:00
|
|
|
args = match.groups()
|
|
|
|
# In both cases, pass any extra_kwargs as **kwargs.
|
|
|
|
kwargs.update(self.default_args)
|
|
|
|
|
2006-08-11 15:01:29 +08:00
|
|
|
return self.callback, args, kwargs
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-08-11 15:01:29 +08:00
|
|
|
def _get_callback(self):
|
|
|
|
if self._callback is not None:
|
|
|
|
return self._callback
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
2007-06-23 13:40:28 +08:00
|
|
|
self._callback = get_callable(self._callback_str)
|
2005-08-01 22:41:01 +08:00
|
|
|
except ImportError, e:
|
2007-06-24 17:38:30 +08:00
|
|
|
mod_name, _ = get_mod_func(self._callback_str)
|
2005-08-01 22:41:01 +08:00
|
|
|
raise ViewDoesNotExist, "Could not import %s. Error was: %s" % (mod_name, str(e))
|
|
|
|
except AttributeError, e:
|
2007-06-24 17:38:30 +08:00
|
|
|
mod_name, func_name = get_mod_func(self._callback_str)
|
2005-08-01 22:41:01 +08:00
|
|
|
raise ViewDoesNotExist, "Tried %s in module %s. Error was: %s" % (func_name, mod_name, str(e))
|
2006-08-11 15:01:29 +08:00
|
|
|
return self._callback
|
|
|
|
callback = property(_get_callback)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-08-11 06:20:18 +08:00
|
|
|
class RegexURLResolver(object):
|
2009-07-17 00:16:13 +08:00
|
|
|
def __init__(self, regex, urlconf_name, default_kwargs=None, app_name=None, namespace=None):
|
2005-08-06 04:50:19 +08:00
|
|
|
# regex is a string representing a regular expression.
|
2009-04-01 00:07:07 +08:00
|
|
|
# urlconf_name is a string representing the module containing URLconfs.
|
2007-07-08 02:24:27 +08:00
|
|
|
self.regex = re.compile(regex, re.UNICODE)
|
2005-08-06 04:50:19 +08:00
|
|
|
self.urlconf_name = urlconf_name
|
2009-01-11 06:54:20 +08:00
|
|
|
if not isinstance(urlconf_name, basestring):
|
|
|
|
self._urlconf_module = self.urlconf_name
|
2006-05-16 12:05:55 +08:00
|
|
|
self.callback = None
|
2006-08-01 10:57:08 +08:00
|
|
|
self.default_kwargs = default_kwargs or {}
|
2009-07-17 00:16:13 +08:00
|
|
|
self.namespace = namespace
|
|
|
|
self.app_name = app_name
|
|
|
|
self._reverse_dict = None
|
|
|
|
self._namespace_dict = None
|
|
|
|
self._app_dict = None
|
2007-06-24 17:38:30 +08:00
|
|
|
|
2007-07-13 17:14:51 +08:00
|
|
|
def __repr__(self):
|
2009-07-17 00:16:13 +08:00
|
|
|
return '<%s %s (%s:%s) %s>' % (self.__class__.__name__, self.urlconf_name, self.app_name, self.namespace, self.regex.pattern)
|
|
|
|
|
|
|
|
def _populate(self):
|
|
|
|
lookups = MultiValueDict()
|
|
|
|
namespaces = {}
|
|
|
|
apps = {}
|
|
|
|
for pattern in reversed(self.url_patterns):
|
|
|
|
p_pattern = pattern.regex.pattern
|
|
|
|
if p_pattern.startswith('^'):
|
|
|
|
p_pattern = p_pattern[1:]
|
|
|
|
if isinstance(pattern, RegexURLResolver):
|
|
|
|
if pattern.namespace:
|
|
|
|
namespaces[pattern.namespace] = (p_pattern, pattern)
|
|
|
|
if pattern.app_name:
|
|
|
|
apps.setdefault(pattern.app_name, []).append(pattern.namespace)
|
|
|
|
else:
|
2008-08-31 19:11:20 +08:00
|
|
|
parent = normalize(pattern.regex.pattern)
|
2008-09-30 13:52:35 +08:00
|
|
|
for name in pattern.reverse_dict:
|
|
|
|
for matches, pat in pattern.reverse_dict.getlist(name):
|
|
|
|
new_matches = []
|
|
|
|
for piece, p_args in parent:
|
|
|
|
new_matches.extend([(piece + suffix, p_args + args) for (suffix, args) in matches])
|
2009-03-12 15:28:22 +08:00
|
|
|
lookups.appendlist(name, (new_matches, p_pattern + pat))
|
2009-07-17 00:16:13 +08:00
|
|
|
for namespace, (prefix, sub_pattern) in pattern.namespace_dict.items():
|
|
|
|
namespaces[namespace] = (p_pattern + prefix, sub_pattern)
|
|
|
|
for app_name, namespace_list in pattern.app_dict.items():
|
|
|
|
apps.setdefault(app_name, []).extend(namespace_list)
|
|
|
|
else:
|
|
|
|
bits = normalize(p_pattern)
|
|
|
|
lookups.appendlist(pattern.callback, (bits, p_pattern))
|
|
|
|
lookups.appendlist(pattern.name, (bits, p_pattern))
|
|
|
|
self._reverse_dict = lookups
|
|
|
|
self._namespace_dict = namespaces
|
|
|
|
self._app_dict = apps
|
|
|
|
|
|
|
|
def _get_reverse_dict(self):
|
|
|
|
if self._reverse_dict is None:
|
|
|
|
self._populate()
|
2007-06-24 17:38:30 +08:00
|
|
|
return self._reverse_dict
|
|
|
|
reverse_dict = property(_get_reverse_dict)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2009-07-17 00:16:13 +08:00
|
|
|
def _get_namespace_dict(self):
|
|
|
|
if self._namespace_dict is None:
|
|
|
|
self._populate()
|
|
|
|
return self._namespace_dict
|
|
|
|
namespace_dict = property(_get_namespace_dict)
|
|
|
|
|
|
|
|
def _get_app_dict(self):
|
|
|
|
if self._app_dict is None:
|
|
|
|
self._populate()
|
|
|
|
return self._app_dict
|
|
|
|
app_dict = property(_get_app_dict)
|
|
|
|
|
2005-08-06 04:50:19 +08:00
|
|
|
def resolve(self, path):
|
2005-08-06 06:22:41 +08:00
|
|
|
tried = []
|
2005-07-13 09:25:57 +08:00
|
|
|
match = self.regex.search(path)
|
|
|
|
if match:
|
|
|
|
new_path = path[match.end():]
|
2009-01-11 06:54:20 +08:00
|
|
|
for pattern in self.url_patterns:
|
2005-08-06 06:22:41 +08:00
|
|
|
try:
|
|
|
|
sub_match = pattern.resolve(new_path)
|
|
|
|
except Resolver404, e:
|
2009-07-02 22:02:58 +08:00
|
|
|
sub_tried = e.args[0].get('tried')
|
|
|
|
if sub_tried is not None:
|
|
|
|
tried.extend([(pattern.regex.pattern + ' ' + t) for t in sub_tried])
|
|
|
|
else:
|
|
|
|
tried.append(pattern.regex.pattern)
|
2005-08-06 06:22:41 +08:00
|
|
|
else:
|
|
|
|
if sub_match:
|
2007-07-08 19:22:53 +08:00
|
|
|
sub_match_dict = dict([(smart_str(k), v) for k, v in match.groupdict().items()])
|
|
|
|
sub_match_dict.update(self.default_kwargs)
|
2007-07-10 20:02:06 +08:00
|
|
|
for k, v in sub_match[2].iteritems():
|
|
|
|
sub_match_dict[smart_str(k)] = v
|
2007-07-08 19:22:53 +08:00
|
|
|
return sub_match[0], sub_match[1], sub_match_dict
|
2005-08-06 06:22:41 +08:00
|
|
|
tried.append(pattern.regex.pattern)
|
|
|
|
raise Resolver404, {'tried': tried, 'path': new_path}
|
2009-07-02 22:02:58 +08:00
|
|
|
raise Resolver404, {'path' : path}
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-08-06 04:50:19 +08:00
|
|
|
def _get_urlconf_module(self):
|
2005-08-11 06:20:18 +08:00
|
|
|
try:
|
|
|
|
return self._urlconf_module
|
|
|
|
except AttributeError:
|
2009-03-19 00:55:59 +08:00
|
|
|
self._urlconf_module = import_module(self.urlconf_name)
|
2005-08-11 06:20:18 +08:00
|
|
|
return self._urlconf_module
|
2005-08-06 04:50:19 +08:00
|
|
|
urlconf_module = property(_get_urlconf_module)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-09-29 21:29:12 +08:00
|
|
|
def _get_url_patterns(self):
|
2009-01-11 06:54:20 +08:00
|
|
|
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
|
|
|
|
try:
|
|
|
|
iter(patterns)
|
|
|
|
except TypeError:
|
2009-04-04 14:54:56 +08:00
|
|
|
raise ImproperlyConfigured("The included urlconf %s doesn't have any "
|
2009-01-11 06:54:20 +08:00
|
|
|
"patterns in it" % self.urlconf_name)
|
|
|
|
return patterns
|
2005-09-29 21:29:12 +08:00
|
|
|
url_patterns = property(_get_url_patterns)
|
|
|
|
|
2005-08-06 04:50:19 +08:00
|
|
|
def _resolve_special(self, view_type):
|
|
|
|
callback = getattr(self.urlconf_module, 'handler%s' % view_type)
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
2009-12-14 04:29:04 +08:00
|
|
|
return get_callable(callback), {}
|
2005-07-19 12:30:23 +08:00
|
|
|
except (ImportError, AttributeError), e:
|
2005-08-06 04:50:19 +08:00
|
|
|
raise ViewDoesNotExist, "Tried %s. Error was: %s" % (callback, str(e))
|
|
|
|
|
|
|
|
def resolve404(self):
|
|
|
|
return self._resolve_special('404')
|
|
|
|
|
|
|
|
def resolve500(self):
|
|
|
|
return self._resolve_special('500')
|
2006-05-16 12:05:55 +08:00
|
|
|
|
2006-08-11 15:01:29 +08:00
|
|
|
def reverse(self, lookup_view, *args, **kwargs):
|
2008-08-31 19:11:20 +08:00
|
|
|
if args and kwargs:
|
|
|
|
raise ValueError("Don't mix *args and **kwargs in call to reverse()!")
|
2007-06-23 13:40:28 +08:00
|
|
|
try:
|
2007-06-25 12:34:34 +08:00
|
|
|
lookup_view = get_callable(lookup_view, True)
|
2008-08-29 03:05:14 +08:00
|
|
|
except (ImportError, AttributeError), e:
|
|
|
|
raise NoReverseMatch("Error importing '%s': %s." % (lookup_view, e))
|
2008-09-27 14:14:11 +08:00
|
|
|
possibilities = self.reverse_dict.getlist(lookup_view)
|
|
|
|
for possibility, pattern in possibilities:
|
|
|
|
for result, params in possibility:
|
|
|
|
if args:
|
|
|
|
if len(args) != len(params):
|
|
|
|
continue
|
|
|
|
unicode_args = [force_unicode(val) for val in args]
|
|
|
|
candidate = result % dict(zip(params, unicode_args))
|
|
|
|
else:
|
|
|
|
if set(kwargs.keys()) != set(params):
|
|
|
|
continue
|
|
|
|
unicode_kwargs = dict([(k, force_unicode(v)) for (k, v) in kwargs.items()])
|
|
|
|
candidate = result % unicode_kwargs
|
|
|
|
if re.search(u'^%s' % pattern, candidate, re.UNICODE):
|
|
|
|
return candidate
|
2009-09-10 19:08:16 +08:00
|
|
|
# lookup_view can be URL label, or dotted path, or callable, Any of
|
|
|
|
# these can be passed in at the top, but callables are not friendly in
|
|
|
|
# error messages.
|
|
|
|
m = getattr(lookup_view, '__module__', None)
|
|
|
|
n = getattr(lookup_view, '__name__', None)
|
|
|
|
if m is not None and n is not None:
|
|
|
|
lookup_view_s = "%s.%s" % (m, n)
|
|
|
|
else:
|
|
|
|
lookup_view_s = lookup_view
|
2008-08-29 03:05:14 +08:00
|
|
|
raise NoReverseMatch("Reverse for '%s' with arguments '%s' and keyword "
|
2009-09-10 19:08:16 +08:00
|
|
|
"arguments '%s' not found." % (lookup_view_s, args, kwargs))
|
2006-06-02 12:38:16 +08:00
|
|
|
|
2006-07-25 15:30:22 +08:00
|
|
|
def resolve(path, urlconf=None):
|
2009-11-16 09:58:00 +08:00
|
|
|
if urlconf is None:
|
|
|
|
urlconf = get_urlconf()
|
2007-06-23 13:40:28 +08:00
|
|
|
return get_resolver(urlconf).resolve(path)
|
2006-07-25 15:30:22 +08:00
|
|
|
|
2009-07-17 00:16:13 +08:00
|
|
|
def reverse(viewname, urlconf=None, args=None, kwargs=None, prefix=None, current_app=None):
|
2009-11-16 09:58:00 +08:00
|
|
|
if urlconf is None:
|
|
|
|
urlconf = get_urlconf()
|
2009-07-17 00:16:13 +08:00
|
|
|
resolver = get_resolver(urlconf)
|
2006-07-25 15:30:22 +08:00
|
|
|
args = args or []
|
|
|
|
kwargs = kwargs or {}
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2008-07-21 15:57:10 +08:00
|
|
|
if prefix is None:
|
|
|
|
prefix = get_script_prefix()
|
2009-07-17 00:16:13 +08:00
|
|
|
|
|
|
|
if not isinstance(viewname, basestring):
|
|
|
|
view = viewname
|
|
|
|
else:
|
|
|
|
parts = viewname.split(':')
|
|
|
|
parts.reverse()
|
|
|
|
view = parts[0]
|
|
|
|
path = parts[1:]
|
|
|
|
|
|
|
|
resolved_path = []
|
|
|
|
while path:
|
|
|
|
ns = path.pop()
|
|
|
|
|
|
|
|
# Lookup the name to see if it could be an app identifier
|
|
|
|
try:
|
|
|
|
app_list = resolver.app_dict[ns]
|
|
|
|
# Yes! Path part matches an app in the current Resolver
|
|
|
|
if current_app and current_app in app_list:
|
|
|
|
# If we are reversing for a particular app, use that namespace
|
|
|
|
ns = current_app
|
|
|
|
elif ns not in app_list:
|
|
|
|
# The name isn't shared by one of the instances (i.e., the default)
|
|
|
|
# so just pick the first instance as the default.
|
|
|
|
ns = app_list[0]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
extra, resolver = resolver.namespace_dict[ns]
|
|
|
|
resolved_path.append(ns)
|
|
|
|
prefix = prefix + extra
|
|
|
|
except KeyError, key:
|
|
|
|
if resolved_path:
|
|
|
|
raise NoReverseMatch("%s is not a registered namespace inside '%s'" % (key, ':'.join(resolved_path)))
|
|
|
|
else:
|
|
|
|
raise NoReverseMatch("%s is not a registered namespace" % key)
|
|
|
|
|
|
|
|
return iri_to_uri(u'%s%s' % (prefix, resolver.reverse(view,
|
2008-07-21 15:57:10 +08:00
|
|
|
*args, **kwargs)))
|
2007-06-23 13:40:28 +08:00
|
|
|
|
2008-06-30 20:34:29 +08:00
|
|
|
def clear_url_caches():
|
|
|
|
global _resolver_cache
|
|
|
|
global _callable_cache
|
|
|
|
_resolver_cache.clear()
|
|
|
|
_callable_cache.clear()
|
2008-07-21 15:57:10 +08:00
|
|
|
|
|
|
|
def set_script_prefix(prefix):
|
|
|
|
"""
|
|
|
|
Sets the script prefix for the current thread.
|
|
|
|
"""
|
|
|
|
if not prefix.endswith('/'):
|
|
|
|
prefix += '/'
|
|
|
|
_prefixes[currentThread()] = prefix
|
|
|
|
|
|
|
|
def get_script_prefix():
|
|
|
|
"""
|
|
|
|
Returns the currently active script prefix. Useful for client code that
|
|
|
|
wishes to construct their own URLs manually (although accessing the request
|
|
|
|
instance is normally going to be a lot cleaner).
|
|
|
|
"""
|
|
|
|
return _prefixes.get(currentThread(), u'/')
|
|
|
|
|
2009-11-16 09:58:00 +08:00
|
|
|
def set_urlconf(urlconf_name):
|
|
|
|
"""
|
|
|
|
Sets the URLconf for the current thread (overriding the default one in
|
|
|
|
settings). Set to None to revert back to the default.
|
|
|
|
"""
|
|
|
|
thread = currentThread()
|
|
|
|
if urlconf_name:
|
|
|
|
_urlconfs[thread] = urlconf_name
|
|
|
|
else:
|
|
|
|
# faster than wrapping in a try/except
|
|
|
|
if thread in _urlconfs:
|
|
|
|
del _urlconfs[thread]
|
|
|
|
|
|
|
|
def get_urlconf(default=None):
|
|
|
|
"""
|
|
|
|
Returns the root URLconf to use for the current thread if it has been
|
|
|
|
changed from the default one.
|
|
|
|
"""
|
|
|
|
thread = currentThread()
|
|
|
|
if thread in _urlconfs:
|
|
|
|
return _urlconfs[thread]
|
|
|
|
return default
|