Ram Rachum
Ram Rachum

Reputation: 88678

Reporting user on Django error reports

I use Django's built-in feature for sending me error reports every time there's a 500 on the server. I use the include_html feature to get the richer version.

I'd really like the logged-in user, if one is logged in, to be sent as part of the report. I know I can deduce it myself using the session id, but I'd really prefer it be sent so I won't have to look it up every time. How can I do that?

Upvotes: 3

Views: 223

Answers (1)

schillingt
schillingt

Reputation: 13731

You need to make use of Custom Error Reports. settings.py You'll need to make your own exception reporter class. I haven't used it with the include_html feature though.

This is an example of what you'll need to do.

from django.shortcuts import render
from django.views.debug import SafeExceptionReporterFilter


class CustomExceptionReporterFilter(SafeExceptionReporterFilter):

    def get_request_repr(self, request):
        result = u"User Info: \n"
        if request.user.is_authenticated():
            user = request.user
            result += u"\nUser Id: {}".format(user.id)
        else:
            result += u"\nUser Id: Not logged in"
        result += super(CustomExceptionReporterFilter, self).get_request_repr(request)
        return result

Upvotes: 1

Related Questions