DEV Community

Vicente G. Reyes
Vicente G. Reyes

Posted on • Updated on

[SOLVED]Why doesn't my Django app display the 404 page even when Debug = False?

I think I've tried everything I found on the internet in the past 2 days and I can't find the right setting to display the error pages.

My views are

...
from django.shortcuts import render, render_to_response
from django.template import RequestContext

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

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

Settings.py

DEBUG = (os.environ.get('DEBUG_VALUE') == 'False')

ALLOWED_HOSTS = ['*', '127.0.0.1']

Main urls.py

handler404 = 'portfolio.views.handler500'
handler500 = 'portfolio.views.handler500'

I use Python 3.7 and Django 2.2

Anyone know this?

This is the link to the Solution

Top comments (11)

Collapse
 
pedromendes96 profile image
Pedro Mendes • Edited

The handlers must be in the main url files. You created the portfolio as your project or is an app? Must be the main url files( the one created with startproject command)

Collapse
 
luabida profile image
Luã Bida Vacaro

you are a lifesaver

Collapse
 
highcenburg profile image
Vicente G. Reyes

Here's my project structure. As on my post, the handlers are on the main urls.py

Collapse
 
pedromendes96 profile image
Pedro Mendes

handler404 = 'portfolio.views.handler500'
handler500 = 'portfolio.views.handler500'

You meant
handler404 = 'portfolio.views.handler404'
handler500 = 'portfolio.views.handler500'

Thread Thread
 
highcenburg profile image
Vicente G. Reyes

oh yeah. I think I already changed that to app.views.handlerX. Still getting the error. It pisses me off.

Thread Thread
 
pedromendes96 profile image
Pedro Mendes

from django.views.default import page_not_found

def handler_404(request, exception):
return page_not_found(request, exception, template_name="errors/404.html")

Thread Thread
 
highcenburg profile image
Vicente G. Reyes

Thank you. There's a little typo on the django.views.default. It should be django.views.defaults.

Anyway, I think it worked. The only question I think I have is if the template should be inside errors or if its ok if the template is inside the template folder together with the other html files.

Thread Thread
 
pedromendes96 profile image
Pedro Mendes

No, that example is from an older project. That path to the template could be any path you want.

Thread Thread
 
highcenburg profile image
Vicente G. Reyes

Got it. Thank you.

Collapse
 
steelwolf180 profile image
Max Ong Zong Bao
Collapse
 
highcenburg profile image
Vicente G. Reyes

Yup, gives me internal server error. I sent a tweet to him but got no reply. Thanks tho.