Ticket #9310: named_404_with_test_2.diff
File named_404_with_test_2.diff, 7.0 KB (added by , 15 years ago) |
---|
-
django/core/urlresolvers.py
223 223 except Resolver404, e: 224 224 sub_tried = e.args[0].get('tried') 225 225 if sub_tried is not None: 226 tried.extend([ (pattern.regex.pattern + ' ' + t)for t in sub_tried])226 tried.extend([[pattern] + t for t in sub_tried]) 227 227 else: 228 tried.append( pattern.regex.pattern)228 tried.append([pattern]) 229 229 else: 230 230 if sub_match: 231 231 sub_match_dict = dict([(smart_str(k), v) for k, v in match.groupdict().items()]) … … 233 233 for k, v in sub_match[2].iteritems(): 234 234 sub_match_dict[smart_str(k)] = v 235 235 return sub_match[0], sub_match[1], sub_match_dict 236 tried.append( pattern.regex.pattern)236 tried.append([pattern]) 237 237 raise Resolver404({'tried': tried, 'path': new_path}) 238 238 raise Resolver404({'path' : path}) 239 239 -
django/views/debug.py
771 771 </p> 772 772 <ol> 773 773 {% for pattern in urlpatterns %} 774 <li>{{ pattern }}</li> 774 <li> 775 {% for pat in pattern %} 776 {{ pat.regex.pattern }} 777 {% if forloop.last and pat.name %}[name='{{ pat.name }}']{% endif %} 778 {% endfor %} 779 </li> 775 780 {% endfor %} 776 781 </ol> 777 782 <p>The current URL, <code>{{ request_path|escape }}</code>, didn't match any of these.</p> -
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/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 -
tests/regressiontests/urlpatterns_reverse/tests.py
17 17 import unittest 18 18 19 19 from django.conf import settings 20 from django.core.urlresolvers import reverse, resolve, NoReverseMatch, Resolver404 20 from django.core.urlresolvers import reverse, resolve, NoReverseMatch, \ 21 Resolver404, RegexURLResolver, \ 22 RegexURLPattern 21 23 from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect 22 24 from django.shortcuts import redirect 23 25 from django.test import TestCase … … 132 134 self.assertRaises(Resolver404, resolve, 'a') 133 135 self.assertRaises(Resolver404, resolve, '\\') 134 136 self.assertRaises(Resolver404, resolve, '.') 137 138 def test_404_tried_urls_have_names(self): 139 """ 140 Verifies that the list of URLs that come back from a Resolver404 141 exception contains a list in the right format for printing out in 142 the DEBUG 404 page with both the patterns and URL names, if available. 143 """ 144 urls = 'regressiontests.urlpatterns_reverse.named_urls' 145 # this list matches the expected URL types and names returned when 146 # you try to resolve a non-existent URL in the first level of included 147 # URLs in named_urls.py (e.g., '/included/non-existent-url') 148 url_types_names = [ 149 [{'type': RegexURLPattern, 'name': 'named-url1'}], 150 [{'type': RegexURLPattern, 'name': 'named-url2'}], 151 [{'type': RegexURLPattern, 'name': None}], 152 [{'type': RegexURLResolver}, {'type': RegexURLPattern, 'name': 'named-url3'}], 153 [{'type': RegexURLResolver}, {'type': RegexURLPattern, 'name': 'named-url4'}], 154 [{'type': RegexURLResolver}, {'type': RegexURLPattern, 'name': None}], 155 [{'type': RegexURLResolver}, {'type': RegexURLResolver}], 156 ] 157 try: 158 resolve('/included/non-existent-url', urlconf=urls) 159 self.fail('resolve did not raise a 404') 160 except Resolver404, e: 161 # make sure we at least matched the root ('/') url resolver: 162 self.assertTrue('tried' in e.args[0]) 163 tried = e.args[0]['tried'] 164 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']))) 165 for tried, expected in zip(e.args[0]['tried'], url_types_names): 166 for t, e in zip(tried, expected): 167 self.assertTrue(isinstance(t, e['type']), '%s is not an instance of %s' % (t, e['type'])) 168 if 'name' in e: 169 if not e['name']: 170 self.assertTrue(t.name is None, 'Expected no URL name but found %s.' % t.name) 171 else: 172 self.assertEqual(t.name, e['name'], 'Wrong URL name. Expected "%s", got "%s".' % (e['name'], t.name)) 135 173 136 174 class ReverseShortcutTests(TestCase): 137 175 urls = 'regressiontests.urlpatterns_reverse.urls' -
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