← Back to Blog
Celery + Django: The Complete Setup Guide for 2024
# Celery + Django: The Complete Setup Guide for 2024
Celery is essential for any serious Django application. Here's how I set it up for production.
## Installation
```bash
pip install celery redis django-celery-beat django-celery-results
```
## Celery App Configuration
```python
# config/celery.py
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.local')
app = Celery('myproject')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()
```
## Settings
```python
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
```
## Writing Tasks
```python
from celery import shared_task
@shared_task(bind=True, max_retries=3)
def send_welcome_email(self, user_id):
try:
user = User.objects.get(pk=user_id)
# send email logic
except Exception as exc:
raise self.retry(exc=exc, countdown=60)
```