Search code examples
djangoapachemod-wsgicustom-error-pages

Django Custom Error Handlers in Apache and mod_wsgi


I have custom 404 and 500 error handlers setup in django and they work in development, but with Apache they just show the default apache Internal Server Error page, even for 404 errors.

Here is my urls.py:

handler400 = 'views.error400'
handler403 = 'views.error403'
handler404 = 'views.error404'
handler500 = 'views.error500'

I've read of many ways to set up custom error pages in apache, but all of the approaches I tried with django did not work. What is the correct way to set this up for Apache/mod_wsgi and Django?

Do I remove those lines from my urls.py and add some lines to my apache.conf?

What lines should be added to apache.conf as I'm not sure how to access views from the apache configuration file and the normal urls.py routing for Django is not working for the custom error pages since as I suspect Apache is intercepting the requests and providing it's own pages.

I just want to use my templates for my custom error pages (which include inheriting from a base template).

How can I achieve this, I can't find a decent answer anywhere?

Thanks

EDIT:

I tried editting the Apache config to add ErrorDocument 500 /templates/500.html and such but it did not work.

I currently have the error page in my templates directory, they each inherit from a base template. Also, they each have their own view to handle the responses.

But no matter what I do, removing those lines from urls.py, adding stuff to apache config, etc. I can't get anything but the typical Internal Server Error apache page to display on any error, even one's that aren't 500 errors.

What can I do to get this working? Worked fine on the django development server, but as soon as apache was thrown into the mix I can't get it to work using either apache or django, I must be missing something but what?


Solution

  • You can remove those lines from urls.py. Django will look up to your templates to get those.

    Just put 404.html and 500.html files into your_app/templates/ directory.