Opened 14 years ago

Closed 13 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)

django-smarter-reverse.patch (3.1 KB ) - added by Patryk Zawadzki 14 years ago.
Proposed patch
django-smarter-reverse-tests.patch (2.7 KB ) - added by Patryk Zawadzki 14 years ago.
Tests, assuming that the mentioned URL can be fixed
django-smarter-reverse.2.patch (6.0 KB ) - added by Patryk Zawadzki 13 years ago.
Updated patch containing both the change and the tests

Download all attachments as: .zip

Change History (15)

by Patryk Zawadzki, 14 years ago

Proposed patch

comment:1 by Russell Keith-Magee, 14 years ago

Needs tests: set
Triage Stage: UnreviewedAccepted

comment:2 by Patryk Zawadzki, 14 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 Russell Keith-Magee, 14 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 Patryk Zawadzki, 14 years ago

Tests, assuming that the mentioned URL can be fixed

comment:4 by Patryk Zawadzki, 14 years ago

Needs tests: unset

comment:5 by Patryk Zawadzki, 14 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 Michał Sałaban, 14 years ago

Can we have this patch applied to 1.3? This change is backwards-compatible and very useful too.

comment:7 by Luke Plant, 13 years ago

Type: Bug

comment:8 by Luke Plant, 13 years ago

Severity: Normal

comment:9 by patchhammer, 13 years ago

Easy pickings: unset
Patch needs improvement: set

django-smarter-reverse-tests.patch fails to apply cleanly on to trunk

by Patryk Zawadzki, 13 years ago

Updated patch containing both the change and the tests

comment:10 by Patryk Zawadzki, 13 years ago

Patch needs improvement: unset

Updated the patch to apply to current SVN trunk.

comment:11 by Jannis Leidel, 13 years ago

Triage Stage: AcceptedReady for checkin

comment:12 by Jannis Leidel, 13 years ago

Resolution: fixed
Status: newclosed

In [16177]:

Fixed #13154 -- Fixed the URL resolver's reverse() to match the behavior of its resolve() with regard to the default kwargs. Many thanks to patrys.

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