Opened 15 years ago
Closed 14 years ago
#13154 closed Bug (fixed)
Make reverse() match the behavior of resolve()
Reported by: | Patryk Zawadzki | Owned by: | nobody |
---|---|---|---|
Component: | Core (Other) | Version: | dev |
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
Consider the following urls.py
:
urlpatterns = patterns('', (r'^faq/$', 'django.views.generic.simple.direct_to_template', {'template': 'faq.html'}, 'faq'), (r'^tos/$', 'django.views.generic.simple.direct_to_template', {'template': 'tos.html'}, 'tos'), )
When passed to resolve()
, /faq/
will yield:
>>> resolve('/faq/') (<function direct_to_template at 0x8b2679c>, (), {'template': 'faq.html'})
However this will raise an exception:
>>> reverse('django.views.generic.simple.direct_to_template', kwargs={'template': 'faq.html'}) NoReverseMatch: Reverse for 'django.views.generic.simple.direct_to_template' with arguments '()' and keyword arguments '{'template': 'faq.html'}' not found.
The attached patch makes reverse()
behave more intuitively and return a match when both the key and value match the default kwargs.
>>> reverse('django.views.generic.simple.direct_to_template', kwargs={'template': 'faq.html'}) '/faq/'
Attachments (3)
Change History (15)
by , 15 years ago
Attachment: | django-smarter-reverse.patch added |
---|
comment:1 by , 15 years ago
Needs tests: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 15 years ago
I'd provide a test but there is a line in tests/regressiontests/urlpatterns_reverse/urls.py
that breaks it:
url(r'^hardcoded/$', 'hardcoded/', empty_view, name="hardcoded"),
I am not quite sure how to interpret that line but it assigns the view to the kwargs. What should I do about it?
comment:3 by , 15 years ago
@patrys -- I can't think of any reason that you would need to pass the view as the kwargs, and if you remove the second 'hardcoded/' argument, the test suite still passes. I'm going to chalk this one up to a bug that didn't get caught because the existing reverse logic didn't walk that particular code path, and there is nothing in the urlpatterns_reverse tests that actually tries to use the view.
Feel free to correct that test as part of your patch for this ticket.
by , 15 years ago
Attachment: | django-smarter-reverse-tests.patch added |
---|
Tests, assuming that the mentioned URL can be fixed
comment:4 by , 15 years ago
Needs tests: | unset |
---|
comment:5 by , 15 years ago
Forgot that Trac doesn't send upload notifications. I've attached the tests and fixed the hardcoded/
one as suggested.
comment:6 by , 14 years ago
Can we have this patch applied to 1.3? This change is backwards-compatible and very useful too.
comment:7 by , 14 years ago
Type: | → Bug |
---|
comment:8 by , 14 years ago
Severity: | → Normal |
---|
comment:9 by , 14 years ago
Easy pickings: | unset |
---|---|
Patch needs improvement: | set |
django-smarter-reverse-tests.patch fails to apply cleanly on to trunk
by , 14 years ago
Attachment: | django-smarter-reverse.2.patch added |
---|
Updated patch containing both the change and the tests
comment:10 by , 14 years ago
Patch needs improvement: | unset |
---|
Updated the patch to apply to current SVN trunk.
comment:11 by , 14 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Proposed patch