错误的urlpatterns

时间:2012-12-12 05:31:07

标签: django

当我尝试加载urlpatterns的不同部分中描述的url时,它会加载相同的视图。显然,我使用的urlpatterns有一个错误,但我找不到它。

这是第一个urls.py文件:

urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', home),
url(r'^comments/', include('django.contrib.comments.urls')),
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
    'document_root': settings.MEDIA_ROOT}),
url(r'^episodes/', include('episodes.urls')),
url(r'^news/', news),
url(r'', include('zinnia.urls')),))

这是episodes.urls中的urlpatterns:

urlpatterns = patterns('',
    (r'$', seasons_list),
    (r'(?P<season>\d{1})/$', episodes_by_season),
    (r'(\d{1})/(\d{1})/$', episode),
)

每当我尝试加载“/ episodes / 1”或“/ episodes / 1/2”等网址时,django只使用seasons_list视图。

1 个答案:

答案 0 :(得分:1)

这是因为所有URL都有一个字符串结尾,因此它们总是匹配第一个模式。添加起始锚点到所有这些锚点,因为包含了URLconf only get the part after the match

  

每当Django遇到include()django.conf.urls.include())时,它会删除与该点匹配的URL的任何部分,并将剩余的字符串发送到包含的URLconf以进行进一步处理。

urlpatterns = patterns('',
    (r'^$', seasons_list),
    (r'^(?P<season>\d{1})/$', episodes_by_season),
    (r'^(\d{1})/(\d{1})/$', episode),
)