动态的设置.py

2024-04-28 08:06:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我用django-constance作为库。 尽管我注意到一件事,当我尝试使用ADMIN和{}时

CONSTANCE_CONFIG = {
'ADMINS': ([('Errors', 'admin@gmail.com')], 'Admin Emails'),
}

发送电子邮件无效。在

在MANAGER中,我尝试过:

^{pr2}$

仍然发送电子邮件是行不通的。我是不是错过了一个错误的实现? 或者你能推荐其他任何可以覆盖ADMIN和{}的库。我使用的是django1.8.5和python3。在

在尝试导入内部时也是如此设置.py它也会产生错误。在


Tags: djangocomconstanceconfigadmin电子邮件错误manager
1条回答
网友
1楼 · 发布于 2024-04-28 08:06:44

1#可能您已经知道,django-constance不支持元组。基本上,很难专门检测元组的小部件 对你来说。管理员可以是added/deleted,所以你怎么可能通过single widget使其成为动态的呢。。!!(想想所有的django小部件)。所以这里 CONSTANCE_ADDITIONAL_FIELDS也不起作用。在

2#我认为你误解了django constance的工作。 它不会刷新您的django server。所以MANAGER = CONSTANCE_CONFIG['ADMINS'][0]是完全错误的(即使使用CONSTANCE_ADDITIONAL_FIELDS)。您在这里访问constant值(不是动态的)。 你需要像

from constance import config
print(config.ADMINS)

3#默认的日志记录配置使用AdminEmailHandler类,它使用django settings中的ADMINS值,而不是constance config。在

因此,一个可能的解决方案可能是创建您自己的handler类,它将使用constance config中的ADMINS值。所以把你的setting.py改成

^{pr2}$

然后创建自己的处理程序类,该类将使用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)

然后更改您的LOGGER配置。如果您没有自定义的LOGGING设置,我建议您从django.utils.log(默认的日志记录)复制默认的记录器配置。并将mail_admins改为

'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
    },

相关问题 更多 >