Django error page not showing

3.1k Views Asked by At

I am using django 1.1.4 and python 2.6.6 with mod_wsgi to build a website

The client wants the 404 and 500 pages to have his logo and home page link. I have written simple pages. Its bigger than 512 bytes. I have paced the files in PROJECT_ROOT/templates/

if I set debug to be True the error says its a 404 error. but when I set it to be False and try to load a page that does not exist I get an internal server error that looks like this:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, root@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log. Apache/2.2.15 (CentOS) Server at www.anglais-verbe.com Port 80

What should I do?

I have not written any views for handling 404, 500 or made changes to the urls.py.

This is my first full fledged django project and I am not sure what is going wrong.

Update:

My admin traceback email is

File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py", line 125, in get_response
   callback, param_dict = resolver.resolve404()

 File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 267, in resolve404
   return self._resolve_special('404')

 File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 259, in _resolve_special
   callback = getattr(self.urlconf_module, 'handler%s' % view_type)

AttributeError: 'module' object has no attribute 'handler404'
2

There are 2 best solutions below

2
On BEST ANSWER

you could set a 404 handler in your main urls.py:

handler404 = 'yourproject.views.file_not_found_404'

then you have to create the following view in your views.py:

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

def file_not_found_404(request):
    #create some variables here if you like
    path = request.path
    response = render_to_response('404.html', locals(),
                              context_instance=RequestContext(request))
    response.status_code = 404
return (response)

then you could create a template for your 404 in the template folder:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL {{ path }} was not found on this server.</p>
<hr>
<address>Apache/2.2.3 (CentOS) Server at yourdomain.com port 80</address>
</body></html>

Hope this helps :) This should also work similiar for the 500 error

3
On
  1. Double check that your template files are named 404.html and 500.html
  2. If debug=FALSE, then default Django behaviour is to send an email to the admins. What does the traceback say?
  3. If you don't have this email, look in the apache error log to see if there are any clues. On Debian (I don't know CentOS off the top of my head), assuming apache2, the location is

    tail /var/log/apache2/error.log