# In tasks.py file
from __future__ import absolute_import
from celery import shared_task
@shared_task
def add(x, y):
return x + y
# In views.py
def home(request):
context = {
'add': tasks.add.delay(5,2)
}
return render(request, "home.html", context)
# In home.html
<h1>{{ add }}</h1>
Instead of 7 appearing on home.html I get giberish that looks like this:313e-44fb-818a-f2b2d824a46b. What can I do to get the data from my tasks.py file?
Simply change:
def home(request):
context = {
'add': tasks.add.delay(5,2)
}
return render(request, "home.html", context)
to:
def home(request):
context = {
'add': tasks.add.delay(5,2).get()
}
return render(request, "home.html", context)
This will be processed by Celery too.
Ading .delay()
You setting task as asynchronous and You receiving task_id
.
More info in doc: Calling Tasks
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With