Ticket #9310: named_404_with_test_2.diff

File named_404_with_test_2.diff, 7.0 KB (added by Cyberj, 15 years ago)

Patch improved to fit with rev12403

  • django/core/urlresolvers.py

     
    223223                except Resolver404, e:
    224224                    sub_tried = e.args[0].get('tried')
    225225                    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])
    227227                    else:
    228                         tried.append(pattern.regex.pattern)
     228                        tried.append([pattern])
    229229                else:
    230230                    if sub_match:
    231231                        sub_match_dict = dict([(smart_str(k), v) for k, v in match.groupdict().items()])
     
    233233                        for k, v in sub_match[2].iteritems():
    234234                            sub_match_dict[smart_str(k)] = v
    235235                        return sub_match[0], sub_match[1], sub_match_dict
    236                     tried.append(pattern.regex.pattern)
     236                    tried.append([pattern])
    237237            raise Resolver404({'tried': tried, 'path': new_path})
    238238        raise Resolver404({'path' : path})
    239239
  • django/views/debug.py

     
    771771      </p>
    772772      <ol>
    773773        {% 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>
    775780        {% endfor %}
    776781      </ol>
    777782      <p>The current URL, <code>{{ request_path|escape }}</code>, didn't match any of these.</p>
  • tests/regressiontests/urlpatterns_reverse/named_urls.py

     
     1from django.conf.urls.defaults import *
     2from views import empty_view
     3
     4urlpatterns = 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

     
     1from django.conf.urls.defaults import *
     2from views import empty_view
     3
     4urlpatterns = 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

     
    1717import unittest
    1818
    1919from django.conf import settings
    20 from django.core.urlresolvers import reverse, resolve, NoReverseMatch, Resolver404
     20from django.core.urlresolvers import reverse, resolve, NoReverseMatch, \
     21                                     Resolver404, RegexURLResolver, \
     22                                     RegexURLPattern
    2123from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
    2224from django.shortcuts import redirect
    2325from django.test import TestCase
     
    132134        self.assertRaises(Resolver404, resolve, 'a')
    133135        self.assertRaises(Resolver404, resolve, '\\')
    134136        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))
    135173
    136174class ReverseShortcutTests(TestCase):
    137175    urls = 'regressiontests.urlpatterns_reverse.urls'
  • tests/regressiontests/urlpatterns_reverse/included_named_urls2.py

     
     1from django.conf.urls.defaults import *
     2from views import empty_view
     3
     4urlpatterns = 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
Back to Top