Celery is a task queue library for Python that allows you to run long-running tasks in the background, while RabbitMQ is a message broker that can be used to handle the communication between Celery and your Django application. Here are the general steps for setting up Celery and RabbitMQ with Django:

  1. Install Celery and the RabbitMQ message broker by running "pip install celery[redis]", "pip install pika" and "pip install django-celery-results" in your command line.
  2. Add Celery to your Django project by including it in the INSTALLED_APPS list in your settings.py file.
  3. Create a new file called "celery.py" in your project's root directory. In this file, you should set up the Celery app and configure it to use RabbitMQ as the message broker. Here's an example of what this file might look like:

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
app = Celery('myproject')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

4. Create a new file called "tasks.py" in your app directory, where you will define your background tasks. Here's an example of what a simple task might look like:

from celery import shared_task

@shared_task
def add(x, y):
    return x + y

5. Start the RabbitMQ message broker by running "rabbitmq-server" in your command line.

6. Start the Celery worker by running "celery -A myproject worker --loglevel=info" in your command line, where "myproject" is the name of your Django project.

7. Run your task in Django views or models using the following:

add.delay(4, 4)


8. To monitor the tasks, you can use Celery Flower by running "celery flower -A myproject --address=127.0.0.1 --port=5555"

That's it! With these steps you have set up Celery and RabbitMQ to work with your Django project. You can now run background tasks asynchronously and use RabbitMQ to handle the communication between your Django application and the Celery worker.