Opened 6 years ago

Closed 18 months ago

Last modified 18 months 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 Changed 6 years ago by Russell Keith-Magee

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
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 Changed 6 years ago by 3point2

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

comment:3 Changed 5 years ago by Julien Phalip

Severity: Normal
Type: Bug

comment:4 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:5 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:6 Changed 4 years ago by Aymeric Augustin

Component: Core (Other)Core (URLs)

comment:7 Changed 3 years ago by cypreess

Owner: changed from nobody to cypreess
Status: newassigned

comment:8 Changed 19 months ago by Bas Peschier

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 Changed 18 months ago by Tim Graham

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

comment:11 Changed 18 months ago by Tim Graham

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 Changed 18 months ago by Tim Graham <timograham@…>

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 Changed 18 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