Opened 6 years ago

Closed 21 months ago

#14772 closed Bug (fixed)

Reversing a URL with optional arguments fails

Reported by: Rob Hudson Owned by: nobody
Component: Core (URLs) Version: 1.2
Severity: Normal Keywords: reverse, sprintdec2010
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I discovered this recently where I wanted a URL to be /detail/ for page 1, and /detail/page-2/ for further pages. I set up the "page-2/" as an optional regular expression and my default had a default kwarg. Reversing this URL without the page worked but not when I added the page argument. So I created some test cases and found some interesting results...

The first test case without the '$' terminated URL, results in:
AssertionError: '/optional/1/2' != '/optional/1/2/'

With the '$' terminated URL, it results in an exception, which is what I was triggering in my project, it seems:
AssertionError: '/optional/1/2/' != <class 'django.core.urlresolvers.NoReverseMatch'>

I'll attach the testing changes...

Attachments (2)

14772.diff (1.9 KB) - added by Rob Hudson 6 years ago.
14772.2.diff (1.9 KB) - added by Rob Hudson 6 years ago.

Download all attachments as: .zip

Change History (9)

Changed 6 years ago by Rob Hudson

Attachment: 14772.diff added

Changed 6 years ago by Rob Hudson

Attachment: 14772.2.diff added

comment:1 Changed 6 years ago by Rob Hudson

The 2nd patch moves the trailing slash out of the named group and the added tests now pass. I'm attaching if these extra tests are something that might be added to the resolving tests.

comment:2 Changed 6 years ago by Julien Phalip

Keywords: sprintdec2010 added
milestone: 1.3
Triage Stage: UnreviewedAccepted

comment:3 Changed 6 years ago by anonymous

milestone: 1.3
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 21 months ago by Bas Peschier

Resolution: fixed
Status: newclosed

The reason the tests failed with the slash inside the capturing group was that you should add the slash to the group in the reverse args/kwargs.

The second patch runs smoothly against master, probably fixed somewhere between then and now.

I am including the tests as part of the tests/documentation of #13525

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