Ticket #9310: named_404_with_test.diff
File named_404_with_test.diff, 7.0 KB (added by , 14 years ago) |
---|
-
django/core/urlresolvers.py
diff -r da07240fd0ca django/core/urlresolvers.py
a b 252 252 except Resolver404, e: 253 253 sub_tried = e.args[0].get('tried') 254 254 if sub_tried is not None: 255 tried.extend([ (pattern.regex.pattern + ' ' + t)for t in sub_tried])255 tried.extend([[pattern] + t for t in sub_tried]) 256 256 else: 257 tried.append( pattern.regex.pattern)257 tried.append([pattern]) 258 258 else: 259 259 if sub_match: 260 260 sub_match_dict = dict([(smart_str(k), v) for k, v in match.groupdict().items()]) … … 262 262 for k, v in sub_match.kwargs.iteritems(): 263 263 sub_match_dict[smart_str(k)] = v 264 264 return ResolverMatch(sub_match.func, sub_match.args, sub_match_dict, sub_match.url_name, self.app_name or sub_match.app_name, [self.namespace] + sub_match.namespaces) 265 tried.append( pattern.regex.pattern)265 tried.append([pattern]) 266 266 raise Resolver404({'tried': tried, 'path': new_path}) 267 267 raise Resolver404({'path' : path}) 268 268 -
django/views/debug.py
diff -r da07240fd0ca django/views/debug.py
a b 778 778 </p> 779 779 <ol> 780 780 {% for pattern in urlpatterns %} 781 <li>{{ pattern }}</li> 781 <li> 782 {% for pat in pattern %} 783 {{ pat.regex.pattern }} 784 {% if forloop.last and pat.name %}[name='{{ pat.name }}']{% endif %} 785 {% endfor %} 786 </li> 782 787 {% endfor %} 783 788 </ol> 784 789 <p>The current URL, <code>{{ request_path|escape }}</code>, didn't match any of these.</p> -
new file tests/regressiontests/urlpatterns_reverse/included_named_urls.py
diff -r da07240fd0ca tests/regressiontests/urlpatterns_reverse/included_named_urls.py
- + 1 from django.conf.urls.defaults import * 2 from views import empty_view 3 4 urlpatterns = patterns('', 5 url(r'^$', empty_view, name="named-url3"), 6 url(r'^extra/(?P<extra>\w+)/$', empty_view, name="named-url4"), 7 url(r'^(?P<one>\d+)|(?P<two>\d+)/$', empty_view), 8 (r'^included/', include('regressiontests.urlpatterns_reverse.included_named_urls2')), 9 ) 10 -
new file tests/regressiontests/urlpatterns_reverse/included_named_urls2.py
diff -r da07240fd0ca tests/regressiontests/urlpatterns_reverse/included_named_urls2.py
- + 1 from django.conf.urls.defaults import * 2 from views import empty_view 3 4 urlpatterns = patterns('', 5 url(r'^$', empty_view, name="named-url5"), 6 url(r'^extra/(?P<extra>\w+)/$', empty_view, name="named-url6"), 7 url(r'^(?P<one>\d+)|(?P<two>\d+)/$', empty_view), 8 ) 9 -
new file tests/regressiontests/urlpatterns_reverse/named_urls.py
diff -r da07240fd0ca tests/regressiontests/urlpatterns_reverse/named_urls.py
- + 1 from django.conf.urls.defaults import * 2 from views import empty_view 3 4 urlpatterns = patterns('', 5 url(r'^$', empty_view, name="named-url1"), 6 url(r'^extra/(?P<extra>\w+)/$', empty_view, name="named-url2"), 7 url(r'^(?P<one>\d+)|(?P<two>\d+)/$', empty_view), 8 (r'^included/', include('regressiontests.urlpatterns_reverse.included_named_urls')), 9 ) -
tests/regressiontests/urlpatterns_reverse/tests.py
diff -r da07240fd0ca tests/regressiontests/urlpatterns_reverse/tests.py
a b 18 18 19 19 from django.conf import settings 20 20 from django.core.exceptions import ImproperlyConfigured 21 from django.core.urlresolvers import reverse, resolve, NoReverseMatch, Resolver404, ResolverMatch 21 from django.core.urlresolvers import reverse, resolve, NoReverseMatch,\ 22 Resolver404, ResolverMatch,\ 23 RegexURLResolver, RegexURLPattern 22 24 from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect 23 25 from django.shortcuts import redirect 24 26 from django.test import TestCase … … 172 174 self.assertRaises(Resolver404, resolve, 'a') 173 175 self.assertRaises(Resolver404, resolve, '\\') 174 176 self.assertRaises(Resolver404, resolve, '.') 177 178 def test_404_tried_urls_have_names(self): 179 """ 180 Verifies that the list of URLs that come back from a Resolver404 181 exception contains a list in the right format for printing out in 182 the DEBUG 404 page with both the patterns and URL names, if available. 183 """ 184 urls = 'regressiontests.urlpatterns_reverse.named_urls' 185 # this list matches the expected URL types and names returned when 186 # you try to resolve a non-existent URL in the first level of included 187 # URLs in named_urls.py (e.g., '/included/non-existent-url') 188 url_types_names = [ 189 [{'type': RegexURLPattern, 'name': 'named-url1'}], 190 [{'type': RegexURLPattern, 'name': 'named-url2'}], 191 [{'type': RegexURLPattern, 'name': None}], 192 [{'type': RegexURLResolver}, {'type': RegexURLPattern, 'name': 'named-url3'}], 193 [{'type': RegexURLResolver}, {'type': RegexURLPattern, 'name': 'named-url4'}], 194 [{'type': RegexURLResolver}, {'type': RegexURLPattern, 'name': None}], 195 [{'type': RegexURLResolver}, {'type': RegexURLResolver}], 196 ] 197 try: 198 resolve('/included/non-existent-url', urlconf=urls) 199 self.fail('resolve did not raise a 404') 200 except Resolver404, e: 201 # make sure we at least matched the root ('/') url resolver: 202 self.assertTrue('tried' in e.args[0]) 203 tried = e.args[0]['tried'] 204 self.assertEqual(len(e.args[0]['tried']), len(url_types_names), 'Wrong number of tried URLs returned. Expected %s, got %s.' % (len(url_types_names), len(e.args[0]['tried']))) 205 for tried, expected in zip(e.args[0]['tried'], url_types_names): 206 for t, e in zip(tried, expected): 207 self.assertTrue(isinstance(t, e['type']), '%s is not an instance of %s' % (t, e['type'])) 208 if 'name' in e: 209 if not e['name']: 210 self.assertTrue(t.name is None, 'Expected no URL name but found %s.' % t.name) 211 else: 212 self.assertEqual(t.name, e['name'], 'Wrong URL name. Expected "%s", got "%s".' % (e['name'], t.name)) 175 213 176 214 class ReverseShortcutTests(TestCase): 177 215 urls = 'regressiontests.urlpatterns_reverse.urls'