0
0
Djangoframework~30 mins

Celery installation and setup in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Celery Installation and Setup in Django
📖 Scenario: You are building a Django web application that needs to perform background tasks like sending emails or processing data without making users wait. To do this, you will set up Celery, a tool that helps run tasks in the background.
🎯 Goal: Learn how to install Celery, configure it with Django, and create a simple background task that can be run asynchronously.
📋 What You'll Learn
Install Celery package
Create a Celery instance in Django project
Configure Celery with Django settings
Write a simple Celery task
💡 Why This Matters
🌍 Real World
Many web applications need to perform time-consuming tasks like sending emails or processing files without making users wait. Celery helps by running these tasks in the background.
💼 Career
Understanding Celery setup is important for backend developers working with Django to build scalable and responsive applications.
Progress0 / 4 steps
1
Install Celery package
Install the Celery package by writing pip install celery in your terminal or command line.
Django
Need a hint?

Use the pip install celery command to add Celery to your project environment.

2
Create Celery instance in Django project
In your Django project folder, create a new file called celery.py. Inside it, write code to create a Celery app instance named app with the project name 'myproject'. Import os and set the default Django settings module to 'myproject.settings'.
Django
Need a hint?

Use os.environ.setdefault to set the Django settings module. Then create a Celery app with Celery('myproject').

3
Configure Celery in Django settings
Open your Django settings.py file. Add a new variable CELERY_BROKER_URL and set it to 'redis://localhost:6379/0' to use Redis as the message broker.
Django
Need a hint?

Set CELERY_BROKER_URL to point to your Redis server URL.

4
Write a simple Celery task
Create a new file called tasks.py inside one of your Django apps. Write a function called add that takes two arguments x and y. Decorate it with @app.task to make it a Celery task. The function should return the sum of x and y.
Django
Need a hint?

Import the Celery app and use @app.task to decorate the add function.