Django没有看到我的网址

时间:2016-01-06 18:30:07

标签: python django python-2.7 django-urls

我得到的网址会导致按日期列出的对象列表,但出于某种原因,我总是得到404。并且在404中表示了URL ...

网址

urlpatterns = patterns('',
....
url(r'^castingListbydate/(?P<year>[0-9])/(?P<month>[0-9])/(?P<day>[0-9])/(?P<type>[0-9])/$', 'app.views.castingListbydate', name='castingListbydate'), 

404消息

Page not found (404)
Request Method:     GET
Request URL:    http://localhost:50862/castingListbydate/2016/1/7/0

Using the URLconf defined in Casting.urls, Django tried these URL patterns, in this order:

    ^$ [name='home']
    ^$ [name='messages_redirect']
    ^inbox/$ [name='messages_inbox']
    ^outbox/$ [name='messages_outbox']
    ^compose/$ [name='messages_compose']
    ^compose/(?P<recipient>[\w.@+-]+)/$ [name='messages_compose_to']
    ^reply/(?P<message_id>[\d]+)/$ [name='messages_reply']
    ^view/(?P<message_id>[\d]+)/$ [name='messages_detail']
    ^delete/(?P<message_id>[\d]+)/$ [name='messages_delete']
    ^undelete/(?P<message_id>[\d]+)/$ [name='messages_undelete']
    ^trash/$ [name='messages_trash']
    ^contact$ [name='contact']
    ^about$ [name='about']
    ^rules$ [name='rules']
    ^typo_create$ [name='typo_create']
    ^castingCard/(?P<id>[0-9])/$ [name='castingCard']
    ^artistBase/(?P<actor>[0-9]{1})/(?P<dancer>[0-9]{1})/(?P<modl>[0-9]{1})/(?P<singer>[0-9]{1})/$ [name='artistBase']
    ^artistSearch$ [name='artistSearch']
    ^artistBases$ [name='artistBases']
    ^actorsBase$ [name='actorsBase']
    ^dancerBase$ [name='dancerBase']
    ^modelsBase$ [name='modelsBase']
    ^vocalBase$ [name='vocalBase']
    ^castingListbydate/(?P<year>[0-9])/(?P<month>[0-9])/(?P<day>[0-9])/(?P<type>[0-9])/$ [name='castingListbydate'] 

只是不明白为什么会发生这种情况

1 个答案:

答案 0 :(得分:4)

您的问题是使用正则表达式模式捕获与日期相关的命名组。

当你这样做时

(?P<year>[0-9])/(?P<month>[0-9])/(?P<day>[0-9])/(?P<type>[0-9])

[0-9]仅匹配1位数。你需要的是,为日期捕获多个数字。

这样的事情:

(?P<year>[0-9]+)/(?P<month>[0-9]+)/(?P<day>[0-9]+)/(?P<type>[0-9]+)

如果您选择更具体,

(?P<year>[0-9]{4})/(?P<month>[0-9]{1, 2})/(?P<day>[0-9]{1, 2})/(?P<type>[0-9]+)

您可以获得更多context on this here