Search code examples
pythondjangodjango-urlsdjango-1.7

Django URL Match Is Not Working


I am having error Page not found (404) while accessing following url:

http://localhost:8000/blog/blogroll/

I am using Python 3.4 & Django 1.7.

Here is my URLS for project:

from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'weblog.views.home', name='home'),
    url(r'^blog/', include('blog.urls', namespace='blog')),
    url(r'^admin/', include(admin.site.urls)),
)

and this is my url for the app "blog":

from django.conf.urls import patterns, url from blog import views

urlpatterns = patterns('',
       url(r'^$', views.index, name='index'),
       url(r'^(?P<slug>\S+)$', views.detail, name='detail'),
       url(r'^blogroll/$', views.blogroll, name='blogroll'),
       #url(r'^(?P<question_id>\d+)/results/$', views.results, name ='results'),
       #url(r'^(?P<question_id>\d+)/vote/$', views.vote, name='vote'),
       )

Besides this the following url is working:

http://localhost:8000/blog/

I am unable to find out where is the mistake. Please advise.

Thanking you in Advance!


Solution

  • Move url(r'^blogroll/$', views.blogroll, name='blogroll'), up one line.

    /blog/anything is matching your (?P<slug>... rule before your blogroll rule ever has a chance to match.

    You should also end the slug regex with / outside the capturing group as django tends to like URLs ending with slashes, and your variable will catch the / if using \S+