Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#7522 closed (invalid)

django.core.urlresolvers.reverse can't accept complex regular expression.

Reported by: bear330 Owned by: nobody
Component: Core (Other) Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I have a url pattern like this:

url(r'^(?P<strategy>\w+)/(?P<scale>(hours|days))/(?P<duration>[1-365]+)$', 'views.index', name='indexDuration')

It will fail when I call:

reverse('indexDuration', args=['trace', 'hours', 24])

the exception raised:

...
File "C:\Program Files\Python25\Lib\site-packages\django\core\urlresolvers.py", line 88, in reverse_helper
    result = re.sub(r'\(([^)]+)\)', MatchChecker(args, kwargs), regex.pattern)
...
error: unbalanced parenthesis

This is related to #6934, but it also raise exception when I change the url to:

url(r'^(?P<strategy>\w+)/(?P<scale>hours)/(?P<duration>[1-365]+)$', 'views.index', name='indexDuration')
>>> reverse('indexDuration', args=['trace', 'hours', 24])
...
File "C:\Program Files\Python25\Lib\site-packages\django\core\urlresolvers.py", line 88, in reverse_helper
    result = re.sub(r'\(([^)]+)\)', MatchChecker(args, kwargs), regex.pattern)
...
NoReverseMatch: Value 24 didn't match regular expression '[1-365]+'

This is a related but other situation to cause reverse raise exception.

Change History (2)

comment:1 follow-up: Changed 7 years ago by dc

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

Remove unneeded parenthesis around (hours|days) and rewrite your ![1-365] part as it is completely broken (it matches only 1,2,3,5,6 digits). Better use \d macros and check bounds in the view.

url(r'^(?P<strategy>\w+)/(?P<scale>hours|days)/(?P<duration>\d+)$', 'views.index', name='indexDuration')

works good.

comment:2 in reply to: ↑ 1 Changed 7 years ago by bear330

Oh, I am sorry about getting confused with regular expression. :)

Thank you.

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