How can I include request.User details in Django to trace email for a production site

I would like to include the contents of request.user in the context details sent by email to the site administrators when an error occurs, as well as the trace and request .GET / POST / COOKIES / META

Any help was appreciated.

+4
source share
2 answers

Since the process_exception middleware receives the request object, you can add any information you want 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
+7
source

Make middleware having a method process_exception. http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-exception

import sys
import traceback
from django.conf import settings
from django.core.mail import mail_admins

class ProcessExceptionMiddleware(object):
    def process_exception(self, request, exception):
        if not settings.DEBUG:
            msg = '\n\n'.join([request.user, request.GET, request.POST, \
                request.COOKIES, request.META, traceback.format_exc(*sys.exc_info())])

            mail_admins("Error!", msg)

, !

+2

All Articles