1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Added the ability to name URL patterns. Helps with disambiguity reverse matches.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4901 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick
2007-04-01 07:25:20 +00:00
parent a071609a70
commit d882656ea3
5 changed files with 83 additions and 17 deletions

View File

@@ -88,7 +88,7 @@ class MatchChecker(object):
return str(value) # TODO: Unicode?
class RegexURLPattern(object):
def __init__(self, regex, callback, default_args=None):
def __init__(self, regex, callback, default_args=None, name=None):
# regex is a string representing a regular expression.
# 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
@@ -100,6 +100,7 @@ class RegexURLPattern(object):
self._callback = None
self._callback_str = callback
self.default_args = default_args or {}
self.name = name
def resolve(self, path):
match = self.regex.search(path)
@@ -205,14 +206,15 @@ class RegexURLResolver(object):
try:
lookup_view = getattr(__import__(mod_name, {}, {}, ['']), func_name)
except (ImportError, AttributeError):
raise NoReverseMatch
if func_name != '':
raise NoReverseMatch
for pattern in self.urlconf_module.urlpatterns:
if isinstance(pattern, RegexURLResolver):
try:
return pattern.reverse_helper(lookup_view, *args, **kwargs)
except NoReverseMatch:
continue
elif pattern.callback == lookup_view:
elif pattern.callback == lookup_view or pattern.name == lookup_view:
try:
return pattern.reverse_helper(*args, **kwargs)
except NoReverseMatch: