diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py index 774e6d3b42..59dcdcb491 100644 --- a/django/core/urlresolvers.py +++ b/django/core/urlresolvers.py @@ -143,6 +143,8 @@ class RegexURLResolver(object): # 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 @@ class RegexURLResolver(object): 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 @@ class RegexURLResolver(object): 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 @@ class RegexURLResolver(object): 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): diff --git a/tests/regressiontests/urlpatterns_reverse/tests.py b/tests/regressiontests/urlpatterns_reverse/tests.py index 379197085a..aecfdfc6c5 100644 --- a/tests/regressiontests/urlpatterns_reverse/tests.py +++ b/tests/regressiontests/urlpatterns_reverse/tests.py @@ -81,6 +81,7 @@ test_data = ( ('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/', [], {}) ) diff --git a/tests/regressiontests/urlpatterns_reverse/urls.py b/tests/regressiontests/urlpatterns_reverse/urls.py index b3aed4f22d..c603c0235a 100644 --- a/tests/regressiontests/urlpatterns_reverse/urls.py +++ b/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 @@ urlpatterns = patterns('', url(r'arg_view/(?P\d+)/$', 'kwargs_view'), url(r'absolute_arg_view/(?P\d+)/$', absolute_kwargs_view), url(r'absolute_arg_view/$', absolute_kwargs_view), + + url('^includes/', include(other_patterns)), ) + +