Having trouble with getting URL config to work
I have an app, which I want to be the root of the website. So, I included the following urlpattern in my main urls.py like so:
(r'^$', include('search.urls')),
In my search urls.py
, I have it set up in the following way:
urlpatterns = patterns('search.views',
(r'^$', 'index'),
(r'^results/$', 'results'),
)
The index urlpattern works, it goes to the index view, but the results urlpattern does not work. I get the following error 开发者_运维百科when I try to access results:
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.
Edit:
main 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}),
)
Try this(put the searchurl include at the end and remove the $):
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')),
)
The reason for this is that the search urls will only be included if your path matches ^$, this means that only the / url will match this pattern since $ marks the end of the pattern. The last line of my urlconf says: forward everything else to the search urlconf (notice the absence of $). Note, anything you place after this line will never be matched, since this line matches anything.
精彩评论