bento
bento

Reputation: 5026

django urlpatterns order

I ran into a problem loading the admin section of my django site with the following error:

No FlatPage matches the given query.

I managed to solve the problem, but I'm attempting to understand why it solved the problem. In my urls.py file, I moved the url(r'', include('django.contrib.flatpages.urls')), after the url(r'^admin/', include(admin.site.urls)), and voila, it worked. Can someone explain, briefly, why this would make a relevant difference? Should the admin url always be at the top of the list?

Code the produced the error:

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^mysite/', include('mysite.foo.urls')),

    url (r'^blog/', include('blog.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'', include('django.contrib.flatpages.urls')),
    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
)

This is django 1.4.

Upvotes: 0

Views: 1811

Answers (1)

Martey
Martey

Reputation: 1631

The problem is that you have not installed the flatpages application correctly. Django's flatpages application relies on a piece of middleware that intercepts 404 requests. As a result, you do not need to add django.contrib.flatpages.urls to your urls.py.

You received that error because the regex you are using ('') matches all URLs. As a result, the urlpattern never reached ^admin/'.

Upvotes: 3

Related Questions