Вот более полный ответ в папке вашего проекта, добавьте новый файл celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE','Proj.settings')
app = Celery('Proj')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
в Proj init.py
from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ('celery_app',)
Это просто настройка по умолчанию для любое приложение сельдерея, для вашего случая в вашем приложении создайте новый файл tasks.py
from celery import shared_task
import time
from django.core.mail import send_mail
@shared_task
def sent_email_to():
time.sleep(10)
send_mail("Your file is ready", "Here is your file", '123@gmail.com',
['example@gmail.com'],fail_silently=False)
return None
, затем в вашем views.py
from .tasks import sent_email_to
def My_Upload(request):
if request.method=="POST":
uploaded_file = request.FILES['document']
fs = FileSystemStorage()
name = fs.save(name=uploaded_file.name,content=uploaded_file)
context = {'Name':name}
sent_email_to()
return render(request,'my_app/homepage.html',context=context)
return render(request,'my_app/upload.html',)
и наконец в settings.py вам нужно добавить
CELERY_BROKER_URL = 'CELERY_SERVER_URL'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
запустить сервер сельдерея, используя
celery -A Proj -l info