#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 , 14 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 14 years ago
i just bumped my head against this limitation, it would be great if there was a fix.
comment:3 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:6 by , 12 years ago
Component: | Core (Other) → Core (URLs) |
---|
comment:7 by , 11 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:8 by , 10 years ago
Needs documentation: | set |
---|---|
Needs tests: | set |
Owner: | changed from | to
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 , 10 years ago
Component: | Core (URLs) → Documentation |
---|---|
Needs documentation: | unset |
Needs tests: | unset |
Type: | Bug → Cleanup/optimization |
comment:11 by , 10 years ago
Summary: | URL Reverser chokes on nested groups → Document how to reverse URL patterns with nested groups |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Looks good, pending some cosmetic comments.
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.