Reputation: 46317
When I get an error exception email from my Django site it would be useful to see the User and/or UserProfile information for the currently logged in user. How do I add this to the Django site exception error emails?
Upvotes: 2
Views: 803
Reputation: 18721
From Django version 1.10 onwards, this is supported natively: see https://docs.djangoproject.com/en/dev/releases/1.10/.
Added request.user to the debug view.
Upvotes: 0
Reputation: 2747
@jsamsa's answer doesn't appear to be true anymore due to changes in Django (at least I don't get a 'LOGNAME' field in error emails).
I am posting an alternative, to receive the full django html error page via email. Details on that are here: https://docs.djangoproject.com/en/dev/topics/logging/#django.utils.log.AdminEmailHandler
and this ticket helpfully points out that you can do this:
LOGGING['handlers']['mail_admins']['include_html'] = True
Upvotes: 4
Reputation: 969
Django appends repr(request) at the end of the e-mail. Using the default wsgi development server you can find the logged in user as
'LOGNAME': 'myuser',
This may be hidden in some e-mail clients as it is wrapped in angle brackets.
<WSGIRequest
GET:<QueryDict: {}>,
POST:<QueryDict: {}>,
...
'LOGNAME': 'myuser',
...
wsgi.run_once': False,
'wsgi.url_scheme': 'http',
'wsgi.version': (1, 0)}>
Also, you can implement a custom middleware that implements the process_exception method:
process_exception(self, request, exception)
request is an HttpRequest object. exception is an Exception object raised by the view function.
Django calls process_exception() when a view raises an exception. process_exception() should return either None or an HttpResponse object. If it returns an HttpResponse object, the response will be returned to the browser. Otherwise, default exception handling kicks in.
Again, middleware are run in reverse order during the response phase, which includes process_exception. If an exception middleware return a response, the middleware classes above that middleware will not be called at all.
Upvotes: 5