我有一个应用程序,我想成为该网站的根。所以,我在我的主urls.py中包含了以下urlpattern,如下所示:
(r'^$', include('search.urls')),
在我的搜索urls.py
中,我按以下方式进行设置:
urlpatterns = patterns('search.views',
(r'^$', 'index'),
(r'^results/$', 'results'),
)
索引urlpattern工作,它进入索引视图,但结果urlpattern不起作用。我尝试访问结果时收到以下错误:
Using the URLconf defined in textbook.urls, Django tried these URL patterns, in this order: 1. ^books/ 2. ^$ 3. ^admin/ 4. ^static/(?P.*)$ The current URL, results/, didn't match any of these.
编辑:
主urls.py
:
urlpatterns = patterns('',
(r'^books/', include('books.urls')),
(r'^$', include('search.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)
答案 0 :(得分:5)
试试这个(把搜索结果包含在最后并删除$):
urlpatterns = patterns('',
(r'^books/', include('books.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
(r'^', include('search.urls')),
)
原因是只有当您的路径与^ $匹配时才会包含搜索网址,这意味着只有/ url才会匹配此模式,因为$标记了模式的结尾。我的urlconf的最后一行说:将所有其他内容转发给搜索urlconf(注意缺少$)。请注意,此行之后的任何内容都将永远不会匹配,因为此行与任何内容都匹配。