How to add request.user to HTTP 500 error reports on Django?

Continuing on from the last post, django provides an excellent hook in middlewares to process exception. You can add a new middleware and override the process_handler exception to add any kind of info to the request object. This info then shows up on the errors displayed on the screen(in debug mode) or sent as email. Profit!

class ExceptionUserInfoMiddleware(object):
    Adds user details to request.META, so that they show up in
    the error emails.

    Add to settings.MIDDLEWARE_CLASSES and keep it outermost
    (i.e. on top if possible). This allows it to catch exceptions 
    in other middlewares as well.

    def process_exception(self, request, exception):
        Process the exception.

           - `request`: request that caused the exception
           - `exception`: actual exception being raised

            if request.user.is_authenticated():
                request.META['USERNAME'] = str(request.user.username)
                request.META['USER_EMAIL'] = str(

You can also find the latest version of this snippet at