Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

In urlconfs, include() may now be used on an iterable of patterns ins…

…tead of just a module string. Refs #6470 -- making the admin use a urlconf is much easier with this work done. Thanks, Alex Gaynor.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9728 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c01098e9cbd572eb2af97b938a7149e187561009 1 parent 299e1e8
@jacobian jacobian authored
View
16 django/core/urlresolvers.py
@@ -143,6 +143,8 @@ def __init__(self, regex, urlconf_name, default_kwargs=None):
# urlconf_name is a string representing the module containing urlconfs.
self.regex = re.compile(regex, re.UNICODE)
self.urlconf_name = urlconf_name
+ if not isinstance(urlconf_name, basestring):
+ self._urlconf_module = self.urlconf_name
self.callback = None
self.default_kwargs = default_kwargs or {}
self._reverse_dict = MultiValueDict()
@@ -151,8 +153,8 @@ def __repr__(self):
return '<%s %s %s>' % (self.__class__.__name__, self.urlconf_name, self.regex.pattern)
def _get_reverse_dict(self):
- if not self._reverse_dict and hasattr(self.urlconf_module, 'urlpatterns'):
- for pattern in reversed(self.urlconf_module.urlpatterns):
+ if not self._reverse_dict:
+ for pattern in reversed(self.url_patterns):
p_pattern = pattern.regex.pattern
if p_pattern.startswith('^'):
p_pattern = p_pattern[1:]
@@ -176,7 +178,7 @@ def resolve(self, path):
match = self.regex.search(path)
if match:
new_path = path[match.end():]
- for pattern in self.urlconf_module.urlpatterns:
+ for pattern in self.url_patterns:
try:
sub_match = pattern.resolve(new_path)
except Resolver404, e:
@@ -200,7 +202,13 @@ def _get_urlconf_module(self):
urlconf_module = property(_get_urlconf_module)
def _get_url_patterns(self):
- return self.urlconf_module.urlpatterns
+ patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
+ try:
+ iter(patterns)
+ except TypeError:
+ raise ImproperlyConfigured("The included urlconf %s doesn't have any"
+ "patterns in it" % self.urlconf_name)
+ return patterns
url_patterns = property(_get_url_patterns)
def _resolve_special(self, view_type):
View
1  tests/regressiontests/urlpatterns_reverse/tests.py
@@ -81,6 +81,7 @@
('kwargs_view', '/arg_view/10/', [], {'arg1':10}),
('regressiontests.urlpatterns_reverse.views.absolute_kwargs_view', '/absolute_arg_view/', [], {}),
('regressiontests.urlpatterns_reverse.views.absolute_kwargs_view', '/absolute_arg_view/10/', [], {'arg1':10}),
+ ('non_path_include', '/includes/non_path_include/', [], {})
)
View
8 tests/regressiontests/urlpatterns_reverse/urls.py
@@ -1,6 +1,10 @@
from django.conf.urls.defaults import *
from views import empty_view, absolute_kwargs_view
+other_patterns = patterns('',
+ url(r'non_path_include/$', empty_view, name='non_path_include'),
+)
+
urlpatterns = patterns('',
url(r'^places/(\d+)/$', empty_view, name='places'),
url(r'^places?/$', empty_view, name="places?"),
@@ -51,5 +55,9 @@
url(r'arg_view/(?P<arg1>\d+)/$', 'kwargs_view'),
url(r'absolute_arg_view/(?P<arg1>\d+)/$', absolute_kwargs_view),
url(r'absolute_arg_view/$', absolute_kwargs_view),
+
+ url('^includes/', include(other_patterns)),
)
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.