【Django】有效解决django.core.exceptions.ImproperlyConfigured: Requested setting EMAIL_FROM, but settings

一、问题描述

在使用celery进行异步邮箱验证时,定义好异步任务和配置好celery的相关设置后,使用celery -A celery_tasks.main worker -l INFO命令能正确启动celery,如下:
【Django】有效解决django.core.exceptions.ImproperlyConfigured: Requested setting EMAIL_FROM, but settings
但是一旦进行debug,celery端就报出如下错误:
django.core.exceptions.ImproperlyConfigured: Requested setting EMAIL_FROM, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing setti
ngs.
【Django】有效解决django.core.exceptions.ImproperlyConfigured: Requested setting EMAIL_FROM, but settings

二、分析原因

这是由于django和celery分布式框架属于两个不同的进程,因此不共享一些资源。

三、解决办法

根据错误提示,需要在celery启动文件celery_tasks.main中进行配置,如下所示:
【Django】有效解决django.core.exceptions.ImproperlyConfigured: Requested setting EMAIL_FROM, but settings
再次运行发现celery可以正常工作啦
【Django】有效解决django.core.exceptions.ImproperlyConfigured: Requested setting EMAIL_FROM, but settings