I'm reading Django 3 by example book and in Chapter 7 of the book we use rabbitmq,celery and flower. I configed rabbitmq,celery and flower but there is a few problems my task is a email that it sends after a order is created and the task is executed in celery terminal and in flower flower panel but i cant see the email. and in rabbitmq panel the messages are unacked and the other problem is that broker tab in flower is empty and it does'nt show rabbitmq. Here is the screenshots and my code.
Here is my tasks.py:
from __future__ import absolute_import, unicode_literals
from celery import shared_task
from django.core.mail import send_mail
from .models import Order
@shared_task
def order_created(order_id):
"""Task to send an e-mail notification when order is successfully created."""
order = Order.objects.get(id=order_id)
subject = f'Order nr. {order.id}'
message = f'Dear {order.first_name},\n\n' \
f' You have successfully placed an order.' \
f'Your order ID is {order.id}'
mail_sent = send_mail(subject, message, 'admin@onlineshop.com', [order.email])
return mail_sent
celery config file, celery.py:
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'Onlineshop.settings')
broker = "amqp://test:test@localhost:5672/"
app = Celery('Onlineshop', broker=broker)
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print(f'Request: {self.request!r}')
flowr config file:
# RabbitMQ management api
broker_api = 'http://guest:guest@localhost:5672/api/'
# Enable debug logging
logging = 'DEBUG'
views.py:
def order_create(request):
cart = Cart(request)
if request.method == 'POST':
user = request.user
form = OrderCreateForm(request.POST)
if form.is_valid():
order = form.save(commit=False)
order.user = user
order.save()
for item in cart:
OrderItem.objects.create(order=order, product=item['product'], price=item['price'],
quantity=item['quantity'])
context = {
'order': order,
}
# clear the cart
cart.clear()
# launch asynchronous task
order_created.delay(order.id)
# set the order in the session
request.session['order_id'] = order.id
# redirect for payment
return redirect(reverse('payment:process'))
else:
form = OrderCreateForm()
context = {'cart': cart,
'form': form,
}
return render(request, 'order/create.html', context)
it seems everything is ok because the tasks are recieved in celery and they show too in rabbitmq panel messages but there are unacked but still i can not see the email in my run terminal. My Email setting are correct and i'm using django email backend. i'm on windows 11 and using celery v5.2.2 and rabbitmq v3.9.11