Adding custom 404 page.

I decided to crack on custom 404 and 500 pages on my website. For directories that don't exist and for articles on the blog for which slug didn't exist nginx was spitting out it's default ugly white paged error. 

This was suggested to me by reddit users on /r/djangolearning, you can see my post here.

Firstly, I started with trying to set up nginx to spit out nice pages, this was unfruitful task as I found information on Django error handling capabilities that are server-friendly and easy to set up.

Let's have a look the set up below.

In main urls.py I defined handler 404:

handler404 = 'personal.views.handler404'

In views.py for personal app(this is the one that renders main view:

from django.shortcuts import render_to_response

def handler404(request, exception, template_name="404.html"):
    response = render_to_response("404.html")
    response.status_code = 404
    return response

Pretty much same was done for blog but I only also added 500 handler as error for slugs that didn't exist was 500. Now for every undefined directory on the website we will see below view, nothing special has to be done in templates as no tags required:

 

For nonexistent blog slugs we will now see below:

 

 

I think this is great addition to any website, definetely more user friendly as navigation throughout the website is still possible for the user. 

Adios!