django - 无法通过生产中的邮件拦截404错误(debug = False)

标签 django error-handling

我尝试将django应用程序配置为在我的应用程序投入生产时接收到错误消息(404和500)。
我已阅读Django文档并相应地配置设置。
我在代码中生成404错误,但未收到包含以下设置的电子邮件。

ADMINS = [('John', 'john@example.com'), ]
MANAGERS = [('John', 'john@example.com'), ]
SEND_BROKEN_LINK_EMAILS = True
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
LOGGING = {
    'version': 1,
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler',
            'include_html': True,
        }
    },
}

最佳答案

SEND_BROKEN_LINK_EMAILS设置在版本1.8中已删除,因此无法使用。
您需要在中间件设置中包括django.middleware.common.BrokenLinkEmailsMiddleware:

MIDDLEWARE = [
    'django.middleware.common.BrokenLinkEmailsMiddleware',
     # ... other middleware ...
]

请注意docs的注释:

BrokenLinkEmailsMiddleware must appear before other middleware that intercepts 404 errors, such as LocaleMiddleware or FlatpageFallbackMiddleware. Put it towards the top of your MIDDLEWARE setting.

关于django - 无法通过生产中的邮件拦截404错误(debug = False),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65656260/

相关文章:

python - Django Rest Framework 创建仅用于执行的 REST API

python - 无法分配 "' HR '": "Employee.department"必须是 "Department"实例

python - Django 隐藏重定向

objective-c - JSON : how to deal with double quotations?

error-handling - NodeJS 0.4.2 http.request : can the error event be fired after the response callback has been called?

Python http 错误日志记录

node.js - 处理 CronJob 执行函数中的错误

python - 遍历 JSON 中的字符串数组

html - 嵌套时,使用 css 舍入图像边框不起作用

python-2.7 - TypeError : object of type 'file' has no len()