문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

instead of:

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

use:

r'^(?P<month>\d{2})-(?P<day>\d{2})-(?P<year>\d{4})/$'
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top