#32402: django.urls resolve does not resolve typed paths, like <int:pk>
-----------------------------------------+------------------------
               Reporter:  HM             |          Owner:  nobody
                   Type:  Uncategorized  |         Status:  new
              Component:  Uncategorized  |        Version:  3.1
               Severity:  Normal         |       Keywords:
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+------------------------
 Resolving typed paths like "<int:pk>" doesn't work, see script to
 reproduce below.

 {{{
 import os

 from django.conf import settings
 from django.http import HttpResponse
 from django.urls import path, reverse, resolve

 fname = os.path.splitext(os.path.basename(__file__))[0]
 urlpatterns = [
     path('<int:pk>/', lambda r, **kwargs: HttpResponse('Hello, world!'),
 name='testurl'),
 ]

 if __name__ == "__main__":
     settings.configure(DEBUG=True, MIDDLEWARE_CLASSES=[],
 ROOT_URLCONF=fname)

     # Works
     reverse('testurl', kwargs={'pk': 4})

     # Raises Resolver404
     resolve('<int:pk>/')
 }}}

 This holds for Django 2.2, Django 3.0, Django 3.1 and Django 3.2a1.

 I've pdb'ed my way into the django code and the problem seems to be that
 when `URLResolver.resolve()` gets around to running
 `RoutePattern.match(path)`, the path looks like `"'<int:pk>/'"` but the
 regex it is checked against, stored on the RoutePattern instance,  looks
 like `re.compile('^(?P<pk>[0-9]+)/$')`, which obviously will never match.

 (I'm using `resolve()` in a unittest to check that manually created views
 with externally known paths are properly installed in the urlconf. I get
 the paths to test from `django_extensions`'s `show_urls`.)

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32402>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/045.3c7f7c92ab2327fe1c3ee962b51c1d21%40djangoproject.com.

Reply via email to