曦远 (DealiAxy)一份私人的文字刊物

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 许可协议。转载请注明出处!

相关文章

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 条

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

加载评论中…