Opened 5 years ago

Closed 4 months ago

Last modified 4 months ago

#13525 closed Cleanup/optimization (fixed)

Document how to reverse URL patterns with nested groups

Reported by: nickretallack Owned by: bpeschier
Component: Documentation Version: 1.1
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

## urls.py
from django.conf.urls.defaults import *
from django.http import HttpResponse

urlpatterns = patterns('',
    url(r'^export1\.(?P<format>\w+)$', lambda request: HttpResponse("THIS"), name='this'),
    url(r'^export2(\.(?P<format>\w+))?$', lambda request: HttpResponse("THAT"), name='that'),
)

## paste into python manage.py shell

from django.core.urlresolvers import reverse, resolve

# Both urls resolve
resolve('/export1.json')[0](None).content
resolve('/export2.json')[0](None).content

# These reverse
reverse('this', args=['json'])
reverse('this', kwargs={'format':'json'})

# These don't
reverse('that', args=['json'])
reverse('that', kwargs={'format':'json'})

## Shell session output

>>> from django.core.urlresolvers import reverse, resolve
>>> 
>>> # Both urls resolve
>>> resolve('/export1.json')[0](None).content
'THIS'
>>> resolve('/export2.json')[0](None).content
'THAT'
>>> 
>>> # These reverse
>>> reverse('this', args=['json'])
'/export1.json'
>>> reverse('this', kwargs={'format':'json'})
'/export1.json'
>>> 
>>> # These don't
>>> reverse('that', args=['json'])
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/Cellar/python/2.6.4/lib/python2.6/site-packages/django/core/urlresolvers.py", line 350, in reverse
    *args, **kwargs)))
  File "/usr/local/Cellar/python/2.6.4/lib/python2.6/site-packages/django/core/urlresolvers.py", line 300, in reverse
    "arguments '%s' not found." % (lookup_view_s, args, kwargs))
NoReverseMatch: Reverse for 'that' with arguments '('json',)' and keyword arguments '{}' not found.

Change History (13)

comment:1 Changed 5 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

I have a suspicion that this will be difficult to fix; if it can't be fixed, it should certainly be documented as a limitation.

comment:2 Changed 5 years ago by 3point2

i just bumped my head against this limitation, it would be great if there was a fix.

comment:3 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:4 Changed 3 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:5 Changed 3 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:6 Changed 2 years ago by aaugustin

  • Component changed from Core (Other) to Core (URLs)

comment:7 Changed 18 months ago by cypreess

  • Owner changed from nobody to cypreess
  • Status changed from new to assigned

comment:8 Changed 5 months ago by bpeschier

  • Needs documentation set
  • Needs tests set
  • Owner changed from cypreess to bpeschier

This is very hard to do, since both groups are captured and (if there was a working implementation) should both be given to the reverser. There would be a hard link between both groups since the inner group would influence the outer group. Using an outer non-capturing group will work and is recommended for this use case:

urlpatterns = patterns('',
    url(r'^export1\.(?P<format>\w+)$', lambda request: HttpResponse("THIS"), name='this'),
    url(r'^export2(?:\.(?P<format>\w+))?$', lambda request: HttpResponse("THAT"), name='that'),
)

I can update the tests and document the limitations that nested captured groups are not supported.

comment:10 Changed 5 months ago by timgraham

  • Component changed from Core (URLs) to Documentation
  • Needs documentation unset
  • Needs tests unset
  • Type changed from Bug to Cleanup/optimization

comment:11 Changed 4 months ago by timgraham

  • Summary changed from URL Reverser chokes on nested groups to Document how to reverse URL patterns with nested groups
  • Triage Stage changed from Accepted to Ready for checkin

Looks good, pending some cosmetic comments.

comment:12 Changed 4 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 23a5d64f:

Fixed #13525 -- Added tests and docs for nested parameters in URL patterns.

When reversing, only outer parameters are used if captured parameters are
nested. Added tests to check the edge cases and documentation for the
behavior with an example to avoid it.

comment:13 Changed 4 months ago by Tim Graham <timograham@…>

In 015a9b9:

[1.8.x] Fixed #13525 -- Added tests and docs for nested parameters in URL patterns.

When reversing, only outer parameters are used if captured parameters are
nested. Added tests to check the edge cases and documentation for the
behavior with an example to avoid it.

Backport of 23a5d64f40b0f4a3fbfef7427ca793cb1df1034e from master

Note: See TracTickets for help on using tickets.
Back to Top