您可以通过将filter
添加到mail_admins
并消除特定的django.security.DisallowedHost
日志记录来解决此问题。 Docs.
当一个非有效的主机收到django.core.exceptions.SuspiciousOperation
上升是由django.security.DisallowedHost
记录器记录和mail_admins
日志处理程序在默认情况下进行处理。
通过将django.utils.log.CallbackFilter
筛选器添加到mail_admins
处理程序中,您可以对所需的日志记录进行沉默。在这种情况下,对mail_admins
处理程序进行静音将避免发送电子邮件。
一个例子LOGGING
设置是这样的(我只会添加相关的位,您的设置将可能有更多的条目):
def skip_missing_host(record):
if record.name == 'django.security.DisallowedHost':
# `record.msg` contents are similar to:
# u"Invalid HTTP_HOST header: 'somehost'.
# You may need to add u'somehost' to ALLOWED_HOSTS."
# Add condition to determine when the ERROR should be logged:
if "u'somehost'" in record.msg:
# Returning False indicates that the record shouldn't be handled:
return False
return True
LOGGING = {
'filters': {
'skip_missing_host': {
'()': 'django.utils.log.CallbackFilter',
'callback': skip_missing_host,
},
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false', 'skip_missing_host'],
'class': 'django.utils.log.AdminEmailHandler'
},
},
}
另外,您可以创建自己的django.security.DisallowedHost
处理程序,这将需要在LOGGING
设置中进行配置。
是的,我已经想通了,我希望我错过了设置文件中的东西,比如IGNORE_ALLOWED_HOST。那么我会用这个方法! – GergelyPolonkai