我正在使用芹菜与Django异步发送电子邮件。当我尝试使用任务时,我得到一个错误,说明'str' object has no attribute 'publish_task'
。从日志来看,似乎错误在于芹菜的代码,但我的代码也可能是源代码。以下是我的日志:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/reminder/
Django Version: 1.8.5
Python Version: 3.4.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'crispy_forms',
'bootstrap3_datetime',
'haystack',
'registration',
'tinymce',
'djcelery',
'djcelery_email',
'RemindApp',
'home',
'BookRoom',
'GameDev')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.middleware.locale.LocaleMiddleware')
Traceback:
File "C:UsersTonyDesktop_FilesEnvsReminderVenvlibsite-packagesdjangocorehandlersbase.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UsersTonyDesktop_FilesEnvsReminderVenvlibsite-packagesdjangocontribauthdecorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "C:UsersTonyDesktop_FilesProgrammingDjangoReminderRemindAppviews.py" in index
49. request.user.email)
File "C:UsersTonyDesktop_FilesEnvsReminderVenvlibsite-packagesceleryapptask.py" in apply_async
565. **dict(self._get_exec_options(), **options)
File "C:UsersTonyDesktop_FilesEnvsReminderVenvlibsite-packagesceleryappbase.py" in send_task
350. task_id = P.publish_task(
Exception Type: AttributeError at /reminder/
Exception Value: 'str' object has no attribute 'publish_task'
另外,这是我的任务:
@celery.task
def Mailer_Send(reminder, reminder_title, user, email):
mail = Mailer()
mail.send_messages(
subject = "Time's up! {} is due".format(reminder_title),
template = 'reminder/email.html',
context = {'user': user, 'post': reminder},
to_emails = [email]
)
logger.info('Sending email for reminder: {}'.format(reminder_title))
这里是我尝试使用任务的地方:
Mailer_Send.apply_async(reminder_note,
reminder_note.title,
request.user,
request.user.email)
你不能这么叫。
你需要这样调用apply_async:
Mailer_Send.apply_async(args=[reminder_note,
reminder_note.title,
request.user,
request.user.email])
或
Mailer_Send.delay(reminder_note,
reminder_note.title,
request.user,
request.user.email)
查看文档:调用芹菜任务
您还可以添加一个倒计时变量,该变量将在异步任务运行之前进行倒计时。