Custom error pages in GAE/Django

To allow for custom error pages (404,500) in Django/Google App Engine all you need is this bit of middle-ware that stubs out the request class for you so that it doesn’t blow up.

Add your custom templates to your template directory and name them 404.html, 500.html etc.

Paste the following into middleware/errorpages.py

class DummyUser(object):

    def get_and_delete_messages(self):
        pass

class NeedsUserObject(object):

    def process_request(self, request):
        request.user = DummyUser()

In your settings.py add the NeedsUserObject middleware

MIDDLEWARE_CLASSES = (
  ...
    'middleware.errorpages.NeedsUserObject',
)
Advertisements