Blog

Django 6: What’s New




Django 6: What’s New

Whats New

By Admin

The Big Addition: Built-in Background Tasks

For years, background jobs in Django usually meant adding a third-party tool like Celery or RQ. Django 6 introduces an official Tasks framework that standardizes how background work is defined and queued. It won’t replace every production queue setup, but it gives you a clean, built-in API that fits naturally into a Django project.

Why tasks matter

Many actions shouldn’t slow down a user request—sending emails, processing files, generating reports, or calling external services. Tasks let you move that work out of the request/response cycle.

Defining and queueing a task

Tasks are defined once and queued when needed. The goal is to keep your view code responsive while the work happens separately.

# tasks.py
from django.tasks import task

@task
def log_message(message):
    print(f"Task says: {message}")
# views.py
from .tasks import log_message
from django.http import HttpResponse

def trigger_task(request):
    log_message.enqueue(message="Hello from Django 6")
    return HttpResponse("Task queued")

Task backends

Django provides simple backends for development and testing (like running tasks immediately). In production, you’ll typically use a backend connected to a real queue/worker system.

# settings.py
TASKS = {
    "default": {
        "BACKEND": "django.tasks.backends.immediate.ImmediateBackend",
    }
}

A practical example: sending email

A common use case is sending a welcome email after registration. With tasks, you can queue the email work without making the user wait.

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

@task
def send_welcome_email(user_email, username):
    send_mail(
        subject=f"Welcome {username}",
        message="Thanks for joining.",
        from_email="[email protected]",
        recipient_list=[user_email],
    )
# views.py
from .tasks import send_welcome_email

def register_user(request):
    # user creation logic here
    send_welcome_email.enqueue(
        user_email="[email protected]",
        username="newuser"
    )

Other improvements in Django 6

Content Security Policy (CSP) support

Django 6 adds built-in support for Content Security Policy headers, making it easier to harden apps against injection attacks.

Template partials

Template partials make it easier to create reusable fragments directly in templates, reducing duplication and keeping markup cleaner.

Email and async refinements

Django continues modernizing its email internals and improving async and ORM ergonomics, making day-to-day development smoother.