Skip to content
TURNKEYGNU / LINUX
AppsDocumentationBlogScreenshotsGitHubTurnKey Hub
Home/Blog/Tweaking Django exceptions with custom middleware

From the project

Tweaking Django exceptions with custom middleware

By Alon Swartz13 December 20101 min read
developmentdjangohub

When settings.DEBUG is set to False, exception tracebacks will be sent to settings.ADMINS. To make it simpler to track down how and why the exception was raised, it's beneficial to know which user caused the exception.

It's quite simple to do this using some custom middleware. In the Hub, we include the associated users email address in the exception with the following:

settings.py

MIDDLEWARE_CLASSES = (
    ...
    'hubutils.middleware.ExceptionMiddleware',
)

hubutils/middleware.py

class ExceptionMiddleware(object):
    def process_exception(self, request, exception):
        """include authenticated user email in request.META"""
        if request.user.is_authenticated():
            request.META['USER_EMAIL'] = request.user.email

Keep exploring

Find your next server

Browse ready-to-use appliances by the job you need to do.

Explore the app library
TURNKEYGNU / LINUX

Own your infrastructure without starting from scratch.

Explore TurnKey
AboutDocumentationFAQBlogGitHub
Free and open sourceSecure by defaultReady to deploy