Opened 6 years ago

Closed 15 months ago

#14772 closed Bug (fixed)

Reversing a URL with optional arguments fails

Reported by: robhudson 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


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 robhudson 6 years ago.
14772.2.diff (1.9 KB) - added by robhudson 6 years ago.

Download all attachments as: .zip

Change History (9)

Changed 6 years ago by robhudson

Changed 6 years ago by robhudson

comment:1 Changed 6 years ago by robhudson

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

  • Keywords sprintdec2010 added
  • milestone set to 1.3
  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 5 years ago by anonymous

  • milestone 1.3 deleted
  • Severity set to Normal
  • Type set to Bug

comment:4 Changed 4 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:5 Changed 4 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

comment:6 Changed 3 years ago by aaugustin

  • Component changed from Core (Other) to Core (URLs)

comment:7 Changed 15 months ago by bpeschier

  • Resolution set to fixed
  • Status changed from new to closed

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