Fixed #28766 -- Added ResolverMatch.route.

Co-Authored-By: Xavier Fernandez <xavier.fernandez@polyconseil.fr>
This commit is contained in:
Benjamin Wohlwend 2017-11-02 17:35:41 +01:00 committed by Tim Graham
parent ad191d9e01
commit 79c196cfb2
8 changed files with 65 additions and 8 deletions

View file

@ -1,7 +1,8 @@
from django.urls import path
from django.urls import include, path
from . import views
urlpatterns = [
path('extra/<extra>/', views.empty_view, name='inner-extra'),
path('', include('urlpatterns.more_urls')),
]

View file

@ -0,0 +1,7 @@
from django.urls import re_path
from . import views
urlpatterns = [
re_path(r'^more/(?P<extra>\w+)/$', views.empty_view, name='inner-more'),
]

View file

@ -1,5 +1,5 @@
from django.conf.urls import include
from django.urls import path
from django.urls import path, re_path
from . import views
@ -11,5 +11,7 @@ urlpatterns = [
path('users/', views.empty_view, name='users'),
path('users/<id>/', views.empty_view, name='user-with-id'),
path('included_urls/', include('urlpatterns.included_urls')),
re_path(r'^regex/(?P<pk>[0-9]+)/$', views.empty_view, name='regex'),
path('', include('urlpatterns.more_urls')),
path('<lang>/<path:url>/', views.empty_view, name='lang-and-path'),
]

View file

@ -26,23 +26,48 @@ class SimplifiedURLTests(SimpleTestCase):
self.assertEqual(match.url_name, 'articles-2003')
self.assertEqual(match.args, ())
self.assertEqual(match.kwargs, {})
self.assertEqual(match.route, 'articles/2003/')
def test_path_lookup_with_typed_parameters(self):
match = resolve('/articles/2015/')
self.assertEqual(match.url_name, 'articles-year')
self.assertEqual(match.args, ())
self.assertEqual(match.kwargs, {'year': 2015})
self.assertEqual(match.route, 'articles/<int:year>/')
def test_path_lookup_with_multiple_paramaters(self):
match = resolve('/articles/2015/04/12/')
self.assertEqual(match.url_name, 'articles-year-month-day')
self.assertEqual(match.args, ())
self.assertEqual(match.kwargs, {'year': 2015, 'month': 4, 'day': 12})
self.assertEqual(match.route, 'articles/<int:year>/<int:month>/<int:day>/')
def test_two_variable_at_start_of_path_pattern(self):
match = resolve('/en/foo/')
self.assertEqual(match.url_name, 'lang-and-path')
self.assertEqual(match.kwargs, {'lang': 'en', 'url': 'foo'})
self.assertEqual(match.route, '<lang>/<path:url>/')
def test_re_path(self):
match = resolve('/regex/1/')
self.assertEqual(match.url_name, 'regex')
self.assertEqual(match.kwargs, {'pk': '1'})
self.assertEqual(match.route, '^regex/(?P<pk>[0-9]+)/$')
def test_path_lookup_with_inclusion(self):
match = resolve('/included_urls/extra/something/')
self.assertEqual(match.url_name, 'inner-extra')
self.assertEqual(match.route, 'included_urls/extra/<extra>/')
def test_path_lookup_with_empty_string_inclusion(self):
match = resolve('/more/99/')
self.assertEqual(match.url_name, 'inner-more')
self.assertEqual(match.route, r'^more/(?P<extra>\w+)/$')
def test_path_lookup_with_double_inclusion(self):
match = resolve('/included_urls/more/some_value/')
self.assertEqual(match.url_name, 'inner-more')
self.assertEqual(match.route, r'included_urls/more/(?P<extra>\w+)/$')
def test_path_reverse_without_parameter(self):
url = reverse('articles-2003')