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 许可协议。转载请注明出处!
相关文章
设置Django关闭Debug后的静态文件路由
Django在Debug模式关闭掉后请求静态文件时,返回404相应码,后台的请求url是"GET /static/css/404.css HTTP/1.1" 404 1217,找不到静态文件的url。通过配置静态文件的url和命令行启动来实现静态文件的获取。 URL中的配置 django的静态文件是通过``来管理的。配置静态文件的响应是通过serve()函数数显的。url.py中的配置如下: fr
2021-12-18 · Django · 约 1 分钟
Django中间件之实现Admin后台IP白名单
前言 Django自带的Admin管理后台很方便,但是实际生产环境真的会有挺多安全问题的,在admin的安全防护这方面,我之前就研究实现了给admin加上登录验证码和限流功能,可以参考这篇文章:给Django Admin添加验证码和多次登录尝试限制 不过就在内部使用的话就好很多,因此我们可以通过Django的中间件来实现IP过滤,所以本文中涉及到两个知识点,一个是Django的中间件,另外一个是P
2022-08-26 · Django · 约 1 分钟
Django全文检索实现:HayStack-+-Whoosh-+-Jieba
涉及到的工具 haystack是django的开源搜索框架,该框架支持**Solr**, Elasticsearch, Whoosh, **Xapian搜索引擎,不用更改代码,直接切换引擎,减少代码量。 搜索引擎使用Whoosh,这是一个由纯Python实现的全文搜索引擎,没有二进制文件等,比较小巧,配置比较简单,当然性能自然略低。 中文分词Jieba,由于Whoosh自带的是英文分词,对中文的分
2022-08-26 · Django · 约 1 分钟
Django数据库性能优化之使用Python集合操作
前言 最近有个新需求: 人员基础信息(记作人员A),10w 某种类型的人员信息(记作人员B),1000 要求在后台上(Django Admin)分别展示:已录入A的人员B列表、未录入的人员B列表 团队的DBA提议使用视图可以解决这个问题(不愧是搞数据库的) PS:起先我觉得Django的Model是直接管理数据库表的,使用Model来映射数据库模型怕是有一定的麻烦,不过查了一下资料发现并不会~ 只
2022-08-26 · Django · 约 1 分钟
讨论
共 0 条加载评论中…