Opened 15 years ago

Closed 10 years ago

Last modified 10 years ago

#13525 closed Cleanup/optimization (fixed)

Document how to reverse URL patterns with nested groups

Reported by: Nick Retallack Owned by: Bas Peschier
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 by Russell Keith-Magee, 15 years ago

Triage Stage: UnreviewedAccepted

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 by 3point2, 14 years ago

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

comment:3 by Julien Phalip, 14 years ago

Severity: Normal
Type: Bug

comment:4 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:5 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:6 by Aymeric Augustin, 12 years ago

Component: Core (Other)Core (URLs)

comment:7 by cypreess, 11 years ago

Owner: changed from nobody to cypreess
Status: newassigned

comment:8 by Bas Peschier, 10 years ago

Needs documentation: set
Needs tests: set
Owner: changed from cypreess to Bas Peschier

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 by Tim Graham, 10 years ago

Component: Core (URLs)Documentation
Needs documentation: unset
Needs tests: unset
Type: BugCleanup/optimization

comment:11 by Tim Graham, 10 years ago

Summary: URL Reverser chokes on nested groupsDocument how to reverse URL patterns with nested groups
Triage Stage: AcceptedReady for checkin

Looks good, pending some cosmetic comments.

comment:12 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: assignedclosed

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 by Tim Graham <timograham@…>, 10 years ago

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