Django 6.0 New Features: Task Framework, Template Partials, and CSP
Django, the web framework for web perfectionists, has been in the game since '05, doing wonders and powering sites like Instagram, Pinterest, Spotify, and Robinhood, as well as many others we never hear about.
Now let's talk about Django 6, the latest version of the framework created by Adrian Holovaty and Simon Willison.
As old as this framework is, it is dropping support for older versions of Python — specifically Python 3.11 and below. That means if you're not on Python 3.12+, you can't upgrade. So for you LTS holdouts who love 3.10 and 3.11, it is time to move on. The Django team does not want you anymore, and that is a fact. They want you on the latest stable versions of Python, which, in my opinion, is good.
I don't know about you, but I update with every version of Python because the improvements in the newer versions are not in the old ones.
Task Framework
Let me start by saying: fuck Celery and Django Q2.
There's a new player in the game called Django's task framework. This new task framework is useful for running simple tasks, like sending an email to a user who just signed up. No more having to use Celery just to send basic emails.
So how do you use it? Like this:
from django.core.mail import send_mail
from django.tasks import task
@task
def email_users(emails, subject, message):
return send_mail(subject, message, None, emails)
That is how you define a task. Then in views.py, you use this:
email_users.enqueue(
emails=["user@example.com"],
subject="You have a message",
message="Hello there!",
)
Warning
Do note that this does not outright replace Celery. It is just for simple tasks like sending emails to users — that's it.
You cannot use the Django-provided backend for production. You will have to find a backend to use, and I recommend the package django-tasks.
To install it:
pip install django-tasks
# for uv users
uv add django-tasks
Then change your settings.py like this:
INSTALLED_APPS = [
# ...
"django_tasks",
"django_tasks.backends.database", # Stores tasks in your DB
]
# Built-in (Django 6.0) - development only
TASKS = {
"default": {
"BACKEND": "django.tasks.backends.immediate.ImmediateBackend",
},
}
# Third-party backend for prod
# TASKS = {
# "default": {
# "BACKEND": "django_tasks.backends.database.DatabaseBackend",
# },
# }
Doing this uses your database for the queue, which adds extra load to your DB. If you do not like that approach, you can look for another task backend or just keep using Celery. Also, the worker is still a separate process.
But if all of that is not a hard pass for you, consider using Django 6 for tasks. It means you can remove Celery from your requirements.txt, you pip addicts.
Content Security Policy
Cross-site scripting and unprotected user input are still a real problem on the web. There are multiple ways to prevent this kind of security issue, like validating user input, but Django also just added a feature I am fully in support of: Content Security Policy.
What is Content Security Policy? It is what helps block bad actors from using data injection to mess with your site.
What kind of data are we talking about? Scripts and images. Imagine some bad actor running a script to fetch data from an endpoint with another user's ID. Dangerous, right?
That is why I think this feature matters. It helps keep the web safer, and honestly, I recommend upgrading just for this alone. It is worth the effort of updating Django.
And how do you use it? Go to settings.py and add this:
from django.utils.csp import CSP
# Middleware
MIDDLEWARE = [
# ... other middleware ...
"django.middleware.csp.ContentSecurityPolicyMiddleware",
# ...
]
SECURE_CSP = {
"default-src": [CSP.SELF],
"script-src": [CSP.SELF, CSP.NONCE],
"img-src": [CSP.SELF, "https:"],
}
If you want to learn more about this, click here.
Template Partials
Django is evolving to make developers' lives easier with template partials.
Template partials are used to section pages for re-rendering from network requests fetched from the server. They are mostly used for slicing up a page, so when paired with a library like htmx, they push back the need to move to more frontend-focused frameworks like React — and all the constant refreshing and complexity that comes with that whole multi-page app way of building.
But now that Django has adopted a more component-centric workflow, I want them to go further.
I want them to add something similar to Django-cotton — something for composition that makes it easier to build better UI with the Django template language. Template partials and cotton both share the idea of reusability in Django: one is for slicing, and the other is for composing.
So yeah, adding something like Django-cotton into Django core would be nice. It would extend the template system and make it possible to compose UI in a much cleaner way.
If you want to learn more about template partials, click here.
Conclusion
There are many other changes in the framework, but these are the main updates and the ones I actually have opinions on.
Hit me up on X or email me if you have any questions.
What's your opinion?
Thanks for reading mine, and stay coding.