I'm in the process of upgrading an existing Django application from 1.8 to 1.9 (1.9.4, specifically). I use a custom logging handler to send admin error emails through a third-party email provider (SendGrid).
When executing the runserver
command, the following traceback is generated:
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.
Here are the relevant code snippets for the custom logging class:
'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)
I've looked through the Django documentation ( https://docs.djangoproject.com/en/1.9/topics/logging/ ), as well as StackOverflow and Google, but I've been unable to find a solution as of yet. I understand that models are not to be imported until all apps are "ready", but I don't believe that I am importing any models in this code, so I'm not sure why this error is occurring.
What part of my code is causing this issue? What steps do I need to take to make my custom logging handler compatible with Django 1.9's AppRegistry; specifically, so that I no longer get the "Apps aren't loaded yet" error?
I had a similar problem. I was able to resolve it by importing the settings before importing anything else. Also, take @Alasdair's advice.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.