Sunday 20 March 2011

Creating a custom 404 page in Django CMS

The default 404 (page not found) response outputs the 404.html file in your application templates directory. But what if you want something a bit more user-friendly? You can override the default 404 handler in urls.py:
handler404 = 'application.views.handler404'
Now simply create a views.py module in your application and define a handler404 method:
from cms.views import details

def handler404(request):
return details(request, 'page-not-found')
Finally, via the Django CMS interface create a page with the slug 'page-not-found'. The contents of this page now be displayed in place of the default. I use this page to display a message and the site map to the user.

Make sure you publish the page, otherwise you will get a server error instead!

I would recommend removing all but the necessary permissions on this CMS page, to stop pesky non-super-users from removing it...

1 comment:

  1. Using this the page will return "200 OK", instead of "404 Not Found". You can check my fix here http://goo.gl/hFaUz

    ReplyDelete