DEV Community

Cover image for Django 6 Released: Here's How to Use Its Game-Changing Task Feature
Adeniyi Olanrewaju
Adeniyi Olanrewaju

Posted on

Django 6 Released: Here's How to Use Its Game-Changing Task Feature

Attention Django Developers: Exciting developments have just been announced: Django 6.0 has been released, introducing a highly anticipated feature: built-in task queues.

Eliminate the need for third-party Celery setups and complex Redis configurations. Django now seamlessly integrates task management directly within the framework.
Let me show you what this looks like and how you can use it today.

What Are Django Tasks?

Think of Django Tasks as Django's answer to Celery, but built-in. You can:

  • Run background jobs
  • Process data asynchronously
  • Send emails without blocking requests
  • Handle long-running operations

And the best part? It's all native Django code.

Setting Up Tasks in Django 6

First, make sure you're on Django 6.0+:

pip install Django==6.0
Enter fullscreen mode Exit fullscreen mode

Important: Django 6 requires Python 3.12 or higher! If you're on an older Python version, you'll need to upgrade first.

Your First Django Task

Let's create a simple task that logs a message:

# tasks.py in your app
from django.tasks import task

@task
def log_message(message):
    print(f"Task received: {message}")
    return f"Processed: {message}"
Enter fullscreen mode Exit fullscreen mode

That's it! You've created your first task. The @task decorator tells Django this function should run as a background task.

Running Tasks from Views

Here's how you enqueue tasks from a Django view:

# views.py
from django.http import HttpResponse
from .tasks import log_message

def trigger_task(request):
    # This queues the task to run in background
    log_message.enqueue(message="Hello from Django 6!")

    return HttpResponse("Task enqueued!")
Enter fullscreen mode Exit fullscreen mode

The .enqueue() method adds your task to the queue. Your view returns immediately while the task runs in the background.

Configuring Task Backends

Django 6 comes with two built-in backends:

  1. Immediate Backend (Default) Tasks run right away in the same thread:
# settings.py
TASKS = {
    "default": {
        "BACKEND": "django.tasks.backends.immediate.ImmediateBackend"
    }
}
Enter fullscreen mode Exit fullscreen mode

Good for: Testing and development.

  1. Dummy Backend Tasks don't run at all:
# settings.py
TASKS = {
    "default": {
        "BACKEND": "django.tasks.backends.dummy.DummyBackend"
    }
}
Enter fullscreen mode Exit fullscreen mode

Good for: When you want to test task queuing without execution.

Real-World Example: Email Sender

Let's build something useful - an email sender task:

# tasks.py
from django.tasks import task
from django.core.mail import send_mail

@task
def send_welcome_email(user_email, username):
    subject = f"Welcome, {username}!"
    message = "Thanks for joining our platform."

    send_mail(
        subject=subject,
        message=message,
        from_email="noreply@example.com",
        recipient_list=[user_email]
    )

    return f"Email sent to {user_email}"
Enter fullscreen mode Exit fullscreen mode

Use it in a view:

# views.py
from django.http import JsonResponse
from .tasks import send_welcome_email

def register_user(request):
    # Get user data from request...
    email = request.POST.get('email')
    username = request.POST.get('username')

    # Queue email task (won't block the response)
    send_welcome_email.enqueue(
        user_email=email,
        username=username
    )

    return JsonResponse({"status": "Registration complete!"})
Enter fullscreen mode Exit fullscreen mode

The user gets an immediate response while the email sends in the background.

Need Production-Ready? Use django-tasks

For production use, you'll want a proper backend. The Django community is already building solutions. Here's one option:

pip install django-tasks
Enter fullscreen mode Exit fullscreen mode

Configure it in settings:

# settings.py
INSTALLED_APPS = [
    'django_tasks',
    'django_tasks.backends.database',
    # ... your other apps
]

TASKS = {
    "default": {
        "BACKEND": "django_tasks.backends.database.DatabaseBackend"
    }
}
Enter fullscreen mode Exit fullscreen mode

Run migrations and start the worker:

python manage.py migrate
python manage.py runserver
python manage.py db_worker  # In a separate terminal
Enter fullscreen mode Exit fullscreen mode

Current Limitations

As Django 6 just released, keep in mind:

  1. Built-in backends are basic – Immediate and Dummy only
  2. Community backends are emerging – Watch for django-tasks-redis, django-tasks-rabbitmq
  3. Python 3.12+ required – Time to upgrade!

Wrapping Up

Django 6's built-in tasks are exciting! They won't replace Celery overnight, but they offer a simpler alternative for many use cases.

When to use Django Tasks:

  • Simple background jobs
  • Email sending
  • Data processing
  • When you want minimal setup

Note: As Django 6 is brand new, expect rapid improvements to the task system. Check the official Django documentation for the latest updates.

Top comments (0)