如何在生产站点的 Django 回溯电子邮件中包含 request.User 详细信息

发布于 2024-10-16 18:19:35 字数 113 浏览 2 评论 0原文

我想在发生错误时将 request.user 的内容包含在通过电子邮件发送给站点管理员的上下文详细信息中,以及回溯和 request.GET/POST/COOKIES/META

任何帮助表示赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深巷少女 2024-10-23 18:19:35

因为 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

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
孤芳又自赏 2024-10-23 18:19:35

制作一个具有 process_exception 方法的中间件。
http://docs.djangoproject.com/en/dev/ topic/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)

我希望能给您一些想法!

Make a middleware that has a process_exception method.
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)

I hope that gives you some ideas!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文