Commit c01098e9 authored by Jacob Kaplan-Moss's avatar Jacob Kaplan-Moss
Browse files

In urlconfs, include() may now be used on an iterable of patterns instead of...

In urlconfs, include() may now be used on an iterable of patterns instead 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
parent 299e1e81
Loading
Loading
Loading
Loading
+12 −4
Original line number Diff line number Diff line
@@ -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):
+1 −0
Original line number Diff line number Diff line
@@ -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/', [], {})

)

+8 −0
Original line number Diff line number Diff line
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?"),
@@ -52,4 +56,8 @@ urlpatterns = patterns('',
    url(r'absolute_arg_view/(?P<arg1>\d+)/$', absolute_kwargs_view),
    url(r'absolute_arg_view/$', absolute_kwargs_view),
    
    url('^includes/', include(other_patterns)),

)