我想在发生错误时通过电子邮件发送给网站管理员的上下文详细信息中包含request.user的内容,以及traceback和request.GET / POST / COOKIES / META
任何帮助表示感谢。
答案 0 :(得分:7)
因为process_exception中间件被传递了请求对象,所以你可以添加你想要的任何信息.META
class ErrorMiddleware(object):
"""
Alter HttpRequest objects on Error
"""
def process_exception(self, request, exception):
"""
Add user details.
"""
request.META['USER'] = request.user.username
答案 1 :(得分:2)
制作一个具有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)
我希望能给你一些想法!