loading...

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

highcenburg profile image Vicente G. Reyes Updated on ・1 min read

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

Posted on by:

highcenburg profile

Vicente G. Reyes

@highcenburg

A Self-Directed Learner, a Shopify Partner, Webflow Designer, Django Developer, a Volunteer Developer at Project Website, DEV Community Moderator & a Podcast host.

Discussion

markdown guide
 

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)

 

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

 

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

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

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

from django.views.default import page_not_found

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

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.

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

 
 

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