Django 1.8 how to create my own 404 page?

3.3k views Asked by At

i want to create my own 404 page. In settings.py I have added:

DEBUG = TEMPLATE_DEBUG = False

ALLOWED_HOSTS = ['*',]

In urls.py:

handler404 = 'blog.views.handler404'

In views.py:

def handler404(request):
    return render(request, 'blog/404.html')

Also I have created that 404.html file.

When i start server i write:

python manage.py runserver --insecure

--insecure is to provide static files (otherwise it is nonsense). But if i go non existing page i get:

<h1>Not Found</h1><p>The requested URL /post/9/ was not found on this server.</p>

How do I solve this? I am using Django 1.8 dunno if this changes anything

1

There are 1 answers

1
JustinP On

You shouldn't need anything in urls.py. Go to your root views.py and add your handler404 method there, and leave urls.py alone.

Ref: Django, creating a custom 500/404 error page

Also, I don't see your TEMPLATE_DIRS variable, i.e.

TEMPLATE_DIRS = (
    '/path/to/template/files/',
    '/path/to/more/template/files/' 
)

Need to make sure that your templates in ../blog/.. are getting found properly. Personally I'd add that specifically as a subdirectory.