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. :Parameters: - `request`: request that caused the exception - `exception`: actual exception being raised """ try: if request.user.is_authenticated(): request.META['USERNAME'] = str(request.user.username) request.META['USER_EMAIL'] = str(request.user.email) except: pass
You can also find the latest version of this snippet at http://gist.github.com/646372