Django 6 发布
Django 6

原始链接: https://docs.djangoproject.com/en/6.0/releases/6.0/

## Django 更新:安全与后台任务 本次 Django 发布引入了安全和后台处理方面的重大改进。**内容安全策略 (CSP)** 支持现已内置,增强了对跨站脚本 (XSS) 等攻击的防御。开发者可以通过 `SECURE_CSP` 等设置定义可信内容来源,利用 Django 常量以提高清晰度。通过中间件和上下文处理器提供强制执行和监控工具。 此外,Django 现在包含一个 **任务框架**,用于将工作卸载到请求/响应周期之外——非常适合发送电子邮件或数据处理等任务。任务使用 `@task` 装饰器定义,并通过配置的后端进行排队(尽管 Django 不*运行*任务,而是依赖于外部工作进程)。 最后,Django 的电子邮件处理已实现现代化,采用 Python 的 `email.message.EmailMessage` API,以实现更简洁、Unicode 友好的电子邮件编写,取代了旧的遗留方法。

## Django 6 与 Web 框架讨论 - 总结 一个 Hacker News 的讨论围绕 Django 的优势,尤其是在 AI 代码生成兴起的情况下。用户强调 Django 的“开箱即用”方法非常适合快速原型化具有功能性 Web 应用程序,例如身份验证和管理面板,这使得 AI 能够更容易地进行迭代,因为代码库更小。 对话延伸到与其他框架的比较。一些人指出 Rails 在 AI 代码生成(Claude)方面表现更好,而另一些人则赞扬 Django 的可维护性和清晰结构。 许多用户表达了对 Django 寿命长和易用性的喜爱,将其与 Java 中 Spring Boot 的复杂性形成对比。 讨论还涉及不断发展的 Web 开发格局,提到了诸如 Flask 之类的微框架、Javascript 堆栈以及 Go 和 Rust 等语言。一个反复出现的主题是功能齐全的框架与构建自定义解决方案的灵活性之间的权衡。 最后,存在关于传统框架与现代 Javascript 为中心的方法的优缺点的争论,一些人哀叹新技术中简单性和稳定性的丧失。
相关文章

原文

Content Security Policy support

Built-in support for the Content Security Policy (CSP) standard is now available, making it easier to protect web applications against content injection attacks such as cross-site scripting (XSS). CSP allows declaring trusted sources of content by giving browsers strict rules about which scripts, styles, images, or other resources can be loaded.

CSP policies can now be enforced or monitored directly using built-in tools: headers are added via the ContentSecurityPolicyMiddleware, nonces are supported through the csp() context processor, and policies are configured using the SECURE_CSP and SECURE_CSP_REPORT_ONLY settings.

These settings accept Python dictionaries and support Django-provided constants for clarity and safety. For example:

from django.utils.csp import CSP

SECURE_CSP = {
    "default-src": [CSP.SELF],
    "script-src": [CSP.SELF, CSP.NONCE],
    "img-src": [CSP.SELF, "https:"],
}

The resulting Content-Security-Policy header would be set to:

default-src 'self'; script-src 'self' 'nonce-SECRET'; img-src 'self' https:

To get started, follow the CSP how-to guide. For in-depth guidance, see the CSP security overview and the reference docs, which include details about decorators to override or disable policies on a per-view basis.

Background Tasks

Django now includes a built-in Tasks framework for running code outside the HTTP request–response cycle. This enables offloading work, such as sending emails or processing data, to background workers.

The framework provides task definition, validation, queuing, and result handling. Django guarantees consistent behavior for creating and managing tasks, while the responsibility for running them continues to belong to external worker processes.

Tasks are defined using the task() decorator:

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)

Once defined, tasks can be enqueued through a configured backend:

email_users.enqueue(
    emails=["[email protected]"],
    subject="You have a message",
    message="Hello there!",
)

Backends are configured via the TASKS setting. The two built-in backends included in this release are primarily intended for development and testing.

Django handles task creation and queuing, but does not provide a worker mechanism to run tasks. Execution must be managed by external infrastructure, such as a separate process or service.

See Django’s Tasks framework for an overview and the Tasks reference for API details.

Adoption of Python’s modern email API

Email handling in Django now uses Python’s modern email API, introduced in Python 3.6. This API, centered around the email.message.EmailMessage class, offers a cleaner and Unicode-friendly interface for composing and sending emails. It replaces use of Python’s older legacy (Compat32) API, which relied on lower-level MIME classes (from email.mime) and required more manual handling of message structure and encoding.

Notably, the return type of the EmailMessage.message() method is now an instance of Python’s email.message.EmailMessage. This supports the same API as the previous SafeMIMEText and SafeMIMEMultipart return types, but is not an instance of those now-deprecated classes.

联系我们 contact @ memedata.com