I need help with reverse and url patterns

12 views
Skip to first unread message

Joshua Russo

unread,
Aug 20, 2011, 3:20:12 PM8/20/11
to django...@googlegroups.com
I setup this patter in url.py

    url(r'^events/(?P<year>\d{4})/(?P<month>\d{2})/$', events, name="eventsMonth")

in my template I have this tag

    {% url eventsMonth year=month.prev.year month=month.prev.month %}

but I keep getting 

    Caught NoReverseMatch while rendering: Reverse for 'eventsMonth' with arguments '()' and keyword arguments '{'month': 7, 'year': 2011}' not found.

I traced the problem into the reverse() code and the logic goes all the way to the end, when the following statement from line 325 of core/urlresolvers.py fails and I can't figure out why

    if re.search(u'^%s' % pattern, candidate, re.UNICODE):

The pattern is 
    'events/(?P<year>\\d{4})/(?P<month>\\d{2})/$'
and the candidate is 
    u'events/2011/7/'

Why doesn't that regular expression match? What am I doing wrong?

Joshua Russo

unread,
Aug 20, 2011, 3:26:28 PM8/20/11
to django...@googlegroups.com
Never mind I got it. I changed my url patter to 

    url(r'^events/(?P<year>\d{4})/(?P<month>\d{1,2})/$', events, name="eventsMonth")

I'm still not very sharp on my regex skills. I realized that {2} required a 2 digit number

Daniel Roseman

unread,
Aug 20, 2011, 3:27:27 PM8/20/11
to django...@googlegroups.com
Because there's only one digit in the month (7), and it's expecting two. 

Either change your code to pad the month to two digits, or change the regex to /(?P<month>\d{1,2})/$

--
DR.
Reply all
Reply to author
Forward
0 new messages