Search code examples
djangodebuggingproduction-environment

How can I include the request.User details in Django's traceback email for a production site


I would like to include the contents of request.user in the context details emailed to the site admins when an error occurs, as well as the traceback and request.GET/POST/COOKIES/META

Any help appreciated.


Solution

  • Because process_exception middleware gets passed the request object, you can add whatever info you like to request.META

    class ErrorMiddleware(object):
        """
        Alter HttpRequest objects on Error
        """
    
        def process_exception(self, request, exception):
            """
            Add user details.
            """
            request.META['USER'] = request.user.username