返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
Django

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.

曦远 (DealiAxy)约 9 分钟
目录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_CSP and SECURE_CSP_REPORT_ONLY settings (Python dict format is supported).
  • Middleware: Provides ContentSecurityPolicyMiddleware to 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 AsyncPaginator and AsyncPage classes, further rounding out the ecosystem for async views and database operations.

Database and GIS

  • Expanded StringAgg support: The StringAgg aggregate 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中间件之实现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 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…