Hands-on Django 6.0: Template Partials, Background Tasks, CSP Security — Experience Report on Three Major Features
Heh, they didn't even squeeze WebSockets in and they dare call this a major version update? Still, template partials and built-in background tasks finally make Django feel like a modern framework—at least when writing HTMX you don't have to scatter fragment files everywhere. Native CSP? Should've had it long ago. As for dropping Python 3.11—versions that can't keep up with the times are just dead weight anyway.
目录11 个章节
Preface
The previous article said I would refocus on Django
Part of the reason is that several new features in Django 6.0 are quite good.
Sorry, I didn't double-check—I got one thing wrong: Django 6.0 has no built-in WebSocket support 🤣
That said, Django 6.0 still brings many nice new features and improvements. After a day's work, I've upgraded the DjangoStarter framework to Django 6.0 and put the new features to use.
This article will briefly introduce the features of this major Django release; I'll cover them in more detail when the next version of DjangoStarter comes out.
Django 6.0
https://docs.djangoproject.com/en/6.0/releases/6.0/
Django 6.0 was released in December 2025, bringing a series of modern tools and improvements. This update mainly focuses on improving the developer experience, especially support for modern web development patterns (such as HTMX), enhanced security, and native background task handling.
Supported Python versions: 3.12, 3.13, 3.14.
Core Features (Headline Features)
1. Template Partials
The Django template language now natively supports template partials. This feature lets developers define and reuse named HTML fragments in the same template file, greatly simplifying component-based development, especially for scenarios that use libraries like HTMX.
- Main change: Introduced the
{% partialdef %}and{% partial %}tags. - Advantage: You no longer need to create a separate file just to reuse a small piece of HTML, nor depend on a third-party package (such as
django-template-partials).
2. Built-in Background Tasks
Django 6.0 introduces a native background task framework that lets you run code outside the HTTP request-response cycle.
- Functionality: You can define and run background tasks directly in Django, for time-consuming operations such as sending email and data processing.
- Significance: Reduces dependence on heavy external task queues like Celery, and is more friendly for small-to-medium projects or simple task scenarios.
3. Native CSP Support
Security is further enhanced with built-in support for Content Security Policy (CSP).
- Configuration: Configure via the
SECURE_CSPandSECURE_CSP_REPORT_ONLYsettings (Python dict format is supported). - Middleware: Provides
ContentSecurityPolicyMiddlewareto automatically add CSP headers. - Flexibility: Supports using nonces via the
csp()context processor, and provides decorators to override or disable the policy at the view level.
Other Important Improvements (Other Improvements)
Modernized Email API
Django 6.0 adopts Python's modern email API and updates the internal email handling mechanism, making it more robust and aligned with modern standards.
Async Enhancements
- Added the
AsyncPaginatorandAsyncPageclasses, further rounding out the ecosystem for async views and database operations.
Database and GIS
- Expanded StringAgg support: The
StringAggaggregate function is no longer limited to PostgreSQL and now supports more database backends. - GIS enhancements: Added new geometry functions and lookups.
Upgrade Notes
- Django 6.0 no longer supports Python 3.11 and below.
- Many features marked as deprecated in the Django 5.x series have been formally removed in 6.0. Please consult the official migration guide before upgrading.
_EOF_
Hands-on Django 6.0: Template Partials, Background Tasks, CSP Security — Experience Report on Three Major Features
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
分享一些2026年有意思的现代化Django生态组件
2026年了,若还在忍受石器时代的模板和冗余CRUD,趁早转行。Cotton和Inertia总算给Django这老古董续了命,Ninja这类工具已是开发及格线。看一眼这些现代化组件吧,别再用那些自我感动的低效代码来折磨审美了。
2026-01-21 · Django · 约 1 分钟
比Django官方实现更好的分页组件+Bootstrap整合
前言 Django全家桶自带的分页组件只能说能满足分页这个功能,但是没那么好用就是了 Django的分页效果 django-pure-pagination分页效果 使用方法 首先安装: pip install django-pure-pagination 添加到App INSTALLED_APPS = ( ... 'pure_pagination', ) 配置一下,配置说明如图 P
2021-12-18 · Django · 约 1 分钟
DjangoRestFramework框架三种分页功能的实现 - 在DjangoStarter项目模板中封装
前言 继续Django后端开发系列文章。刚好遇到一个分页的需求,就记录一下。 Django作为一个“全家桶”型的框架,本身啥都有,分页组件也是有的,但默认的分页组件没有对API开发做优化,所以DjangoRestFramework这个专门写API的框架又把Django的分页组件包装了一层,集成在viewsets里的时候会更方便。 不过我们不可能一直用viewsets,有一部分API还是要用自由度更
2022-08-26 · Django · 约 1 分钟
Django-Import-Export插件关于外键的处理
前言 Django-Import-Export是一款很好用很方便的Django数据导出导入插件,可以和DjangoAdmin管理后台完美集成,只需要少量的代码配置即可方便实现你要的多种格式导出导入,关于这个插件的使用更多可以看我之前的文章:这里放文章链接 之前我在使用中都是专门做了一个原始数据的表来存导入的数据,然后再对原始数据表做一些数据处理,把数据存到其他表才能真正使用这些数据。(不是很好的做
2022-04-02 · Django · 约 1 分钟
讨论
共 0 条加载评论中…