我正在将现有的Django应用程序从1.8升级到1.9(具体为1.9.4)。我使用自定义日志记录处理程序通过第三方电子邮件提供商(SendGrid)发送管理员错误电子邮件。
执行runserver
命令时,会生成以下回溯:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/coredev/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/Users/coredev/.virtualenvs/django19/lib/python3.4/site-packages/django/core/management/__init__.py", line 327, in execute
django.setup()
File "/Users/coredev/.virtualenvs/django19/lib/python3.4/site-packages/django/__init__.py", line 17, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/Users/coredev/.virtualenvs/django19/lib/python3.4/site-packages/django/utils/log.py", line 71, in configure_logging
logging_config_func(logging_settings)
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/logging/config.py", line 789, in dictConfig
dictConfigClass(config).configure()
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/logging/config.py", line 565, in configure
'%r: %s' % (name, e))
ValueError: Unable to configure handler 'mail_admins': Apps aren't loaded yet.
以下是自定义日志记录类的相关代码段:
'mail_admins': {
'level': 'ERROR',
'class': 'coredev.utils.logging.SendgridAdminEmailHandler',
'filters': ['require_debug_false'],
'include_html': True,
}
from django.utils.log import AdminEmailHandler
from coredev.tasks import sendgrid_email
from settings import ADMINS
class SendgridAdminEmailHandler(AdminEmailHandler):
def send_mail(self, subject, message, *args, **kwargs):
for admin in ADMINS:
try:
if kwargs.get('html_message') is not None:
message_content = kwargs['html_message']
else:
message_content = message
sendgrid_email(admin[1], subject, message_content, message)
except:
pass
import sendgrid
from django.conf import settings
def sendgrid_email(to, subject, body, alt_body, from_email = settings.DEFAULT_FROM_EMAIL, template_id = settings.DEFAULT_TEMPLATE_ID):
sg = sendgrid.SendGridClient(settings.EMAIL_HOST_USER, settings.EMAIL_HOST_PASSWORD, raise_errors = True)
message = sendgrid.Mail()
message.add_filter('templates', 'enable', '1')
message.add_filter('templates', 'template_id', template_id)
message.add_to(to)
message.set_from(from_email)
message.set_subject(subject)
message.set_html(body)
if alt_body:
message.set_text(alt_body)
sg.send(message)
我查看了Django文档(https://docs.djangoproject.com/en/1.9/topics/logging/),以及StackOverflow和Google,但我一直无法找到解决方案。我知道在所有应用程序“准备就绪”之前不会导入模型,但我不相信我在此代码中导入任何模型,所以我不确定为什么会出现此错误。
我的代码的哪一部分导致了这个问题?我需要采取哪些步骤才能使自定义日志记录处理程序与Django 1.9的AppRegistry兼容;特别是,所以我不再得到“应用程序尚未加载”错误?
答案 0 :(得分:0)
我有类似的问题。我可以通过在导入其他任何内容之前导入设置来解决它。另外,请听@ Alasdair的建议。
from django.conf import settings
from django.utils.log import AdminEmailHandler
from coredev.tasks import sendgrid_email
class SendgridAdminEmailHandler(AdminEmailHandler):
def send_mail(self, subject, message, *args, **kwargs):
for admin in settings.ADMINS:
try:
if kwargs.get('html_message') is not None:
message_content = kwargs['html_message']
else:
message_content = message
sendgrid_email(admin[1], subject, message_content, message)
except:
pass