Search code examples
regexdjangourlconf

Django can't match url for date


I have a simple application and would like the home page to take a date as an url parameter.

url(
    regex=r'^$',
    view=HomeView.as_view(),
    name='home'
    ),
url(
    regex=r'^/(?P<date>\d{2}-\d{2}-\d{4})/$',
    view=HomeView.as_view(),
    name='home'
    ), 

But when I am running (on localhost) going to 127.0.0.1:8000/08-01-2013 results in a page not found 404. Is there something wrong with my regular expression?


Solution

  • From the URL dispatcher docs:

    There’s no need to add a leading slash, because every URL has that. For example, it’s ^articles, not ^/articles.

    So the correct regexp (since you say you don't need to break down the date components) is:

    r'^(?P<date>\d{2}-\d{2}-\d{4})/$'
    

    I see that falinsky's answer corrects the leading slash as well.