1#
Probably you already know, django-constance
does not support tuple. Basically it is really hard to detect a widget for tuple specially
in your case. ADMINS can be added/deleted
so how possibly you can make it dynamic through a single widget
..!!(think about all django widgets). So here
CONSTANCE_ADDITIONAL_FIELDS
will also not work.
2#
I think you are misunderstanding the working of django constance.
It does not refresh your django server
. So MANAGER = CONSTANCE_CONFIG['ADMINS'][0]
is totally wrong(even using CONSTANCE_ADDITIONAL_FIELDS
). You accessing constant
value here(not dynamic).
You need to access it like
from constance import config
print(config.ADMINS)
3#
Default logging config uses AdminEmailHandler
class for mail_admins
, which uses ADMINS
value from django settings
, not constance config
.
So one possible solution might be to create your own handler
class which will use ADMINS
value from constance config
. So change your setting.py
to
CONSTANCE_CONFIG = {
'ADMIN1': ('admin@gmail.com', 'This one will receive error on 500'),
} # you can add as many admins as you want with ADMIN1, ADMIN2 etc(no tuple)
then create your own handler class which will use CONSTANCE_CONFIG
.
from django.utils.log import AdminEmailHandler
from constance import config
from django.conf import settings
from django.core.mail.message import EmailMultiAlternatives
class ConstanceEmailHandler(AdminEmailHandler):
def send_mail(self, subject, message, html_message=None, fail_silently=False, *args, **kwargs):
# create a list of ADMIN emails here, if you have more then one ADMIN
mail = EmailMultiAlternatives('%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject),
message, settings.SERVER_EMAIL, [config.ADMIN1],
connection=self.connection())
if html_message:
mail.attach_alternative(html_message, 'text/html')
mail.send(fail_silently=fail_silently)
And then change your LOGGER
config. I would recommend you to copy default logger config from django.utils.log
(DEFAULT_LOGGING) if you do not have your custom LOGGING
setup. And change mail_admins
to
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'], # change it to require_debug_true if you want to test it locally.
'class': '<yourproject>.<yourfile>.ConstanceEmailHandler', # path to newly created handler class
'include_html': True
},