Opened 16 years ago

Closed 16 years ago

Last modified 16 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: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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 by dc, 16 years ago

Resolution: invalid
Status: newclosed

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.

in reply to:  1 comment:2 by bear330, 16 years ago

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

Thank you.

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