Answer the question
In order to leave comments, you need to log in
Django celery not handling background task?
Hello. There is a project on django 2.2 and celery 4.2.2
After registration in the background, I send an email to activate my account. Celery sees the task, writes "received task", but does not execute the function itself.
# view
class SignUpFormView(FormView):
.....
def form_valid(self, form):
current_site = get_current_site(self.request)
mail_subject = 'Активация аккаунта '
to_email = [
form.cleaned_data.get('email'),
]
from_email = settings.EMAIL_HOST_USER
ctx = {
'user': user,
'domain': current_site,
'uid': urlsafe_base64_encode(force_bytes(user.pk)),
'token': account_activation_token.make_token(user),
}
message = render_to_string('mail/acc_active_email.html', ctx)
send_mail_activation.delay(message, mail_subject, from_email, to_email)
return super(SignUpFormView, self).form_valid(form)
# task
@task()
def send_mail_activation(message, mail_subject, from_email, to_email):
msg = EmailMessage(mail_subject, message, from_email, to_email)
msg.content_subtype = 'html'
msg.send()
# settings.py
CELERY_BROKER_URL = 'redis://127.0.0.1:6379/1'
# celery.py
import os
from celery import Celery
from django.conf import settings
# Основыне настройки Django для celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings.settings')
# for Windows
os.environ.setdefault('FORKED_BY_MULTIPROCESSING', '1')
app = Celery('settings')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
# __init__.py
from .celery import app as celery_app
__all__ = ('celery_app',)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question