---
title: "使用 Django"
version: 4.2
locale: zh-hans
source: https://docs.djangoproject.com/zh-hans/4.2/topics/
canonical: https://djangodocs.dev/zh-hans/4.2/topics/
---
# 使用 Django

你需要知道的所有 Django 关键部分介绍：

- [如何安装 Django](/zh-hans/4.2/topics/install/)

  - [安装 Python](/zh-hans/4.2/topics/install/#install-python)
  - [安装 Apache 和 `mod_wsgi`](/zh-hans/4.2/topics/install/#install-apache-and-mod-wsgi)
  - [运行你的数据库](/zh-hans/4.2/topics/install/#get-your-database-running)
  - [安装 Django 源码](/zh-hans/4.2/topics/install/#install-the-django-code)
- [模型和数据库](/zh-hans/4.2/topics/db/)

  - [模型](/zh-hans/4.2/topics/db/models/)
  - [执行查询](/zh-hans/4.2/topics/db/queries/)
  - [聚合](/zh-hans/4.2/topics/db/aggregation/)
  - [搜索](/zh-hans/4.2/topics/db/search/)
  - [管理器](/zh-hans/4.2/topics/db/managers/)
  - [执行原生 SQL 查询](/zh-hans/4.2/topics/db/sql/)
  - [数据库事务](/zh-hans/4.2/topics/db/transactions/)
  - [多数据库](/zh-hans/4.2/topics/db/multi-db/)
  - [表空间（Tablespaces）](/zh-hans/4.2/topics/db/tablespaces/)
  - [数据库访问优化](/zh-hans/4.2/topics/db/optimization/)
  - [数据库工具](/zh-hans/4.2/topics/db/instrumentation/)
  - [辅助工具](/zh-hans/4.2/topics/db/fixtures/)
  - [模型关联 API 用法示例](/zh-hans/4.2/topics/db/examples/)
- [处理 HTTP 请求](/zh-hans/4.2/topics/http/)

  - [URL调度器](/zh-hans/4.2/topics/http/urls/)
  - [编写视图](/zh-hans/4.2/topics/http/views/)
  - [视图装饰器](/zh-hans/4.2/topics/http/decorators/)
  - [文件上传](/zh-hans/4.2/topics/http/file-uploads/)
  - [Django 便捷函数](/zh-hans/4.2/topics/http/shortcuts/)
  - [通用视图](/zh-hans/4.2/topics/http/generic-views/)
  - [中间件](/zh-hans/4.2/topics/http/middleware/)
  - [如何使用会话](/zh-hans/4.2/topics/http/sessions/)
- [使用表单](/zh-hans/4.2/topics/forms/)

  - [HTML表单](/zh-hans/4.2/topics/forms/#html-forms)
  - [Django在表单中的角色](/zh-hans/4.2/topics/forms/#django-s-role-in-forms)
  - [Django 中的表单](/zh-hans/4.2/topics/forms/#forms-in-django)
  - [构建一张表单](/zh-hans/4.2/topics/forms/#building-a-form)
  - [详解Django `Form` 类](/zh-hans/4.2/topics/forms/#more-about-django-form-classes)
  - [使用表单模板](/zh-hans/4.2/topics/forms/#working-with-form-templates)
  - [更多相关主题](/zh-hans/4.2/topics/forms/#further-topics)
- [模板](/zh-hans/4.2/topics/templates/)

  - [Django 模板语言](/zh-hans/4.2/topics/templates/#the-django-template-language)
  - [模板引擎的支持](/zh-hans/4.2/topics/templates/#support-for-template-engines)
- [基于类的视图](/zh-hans/4.2/topics/class-based-views/)

  - [基于类的视图](/zh-hans/4.2/topics/class-based-views/intro/)
  - [内置的基于类的通用视图](/zh-hans/4.2/topics/class-based-views/generic-display/)
  - [使用基于类的视图处理表单](/zh-hans/4.2/topics/class-based-views/generic-editing/)
  - [在基于类的视图中使用混入](/zh-hans/4.2/topics/class-based-views/mixins/)
  - [基础示例](/zh-hans/4.2/topics/class-based-views/#basic-examples)
  - [在 URLconf 中的用法](/zh-hans/4.2/topics/class-based-views/#usage-in-your-urlconf)
  - [子类化通用视图](/zh-hans/4.2/topics/class-based-views/#subclassing-generic-views)
  - [异步类视图](/zh-hans/4.2/topics/class-based-views/#asynchronous-class-based-views)
- [迁移](/zh-hans/4.2/topics/migrations/)

  - [命令](/zh-hans/4.2/topics/migrations/#the-commands)
  - [后端支持](/zh-hans/4.2/topics/migrations/#backend-support)
  - [工作流程](/zh-hans/4.2/topics/migrations/#workflow)
  - [事务](/zh-hans/4.2/topics/migrations/#transactions)
  - [依赖](/zh-hans/4.2/topics/migrations/#dependencies)
  - [迁移文件](/zh-hans/4.2/topics/migrations/#migration-files)
  - [向应用添加迁移](/zh-hans/4.2/topics/migrations/#adding-migrations-to-apps)
  - [撤销迁移](/zh-hans/4.2/topics/migrations/#reversing-migrations)
  - [历史模型](/zh-hans/4.2/topics/migrations/#historical-models)
  - [删除模型字段时的注意事项](/zh-hans/4.2/topics/migrations/#considerations-when-removing-model-fields)
  - [数据迁移](/zh-hans/4.2/topics/migrations/#data-migrations)
  - [压缩迁移](/zh-hans/4.2/topics/migrations/#squashing-migrations)
  - [序列化值](/zh-hans/4.2/topics/migrations/#serializing-values)
  - [支持多个 Django 版本](/zh-hans/4.2/topics/migrations/#supporting-multiple-django-versions)
- [管理文件](/zh-hans/4.2/topics/files/)

  - [在模型中使用文件](/zh-hans/4.2/topics/files/#using-files-in-models)
  - [`File` 对象](/zh-hans/4.2/topics/files/#the-file-object)
  - [文件存储](/zh-hans/4.2/topics/files/#file-storage)
- [Django 中的测试](/zh-hans/4.2/topics/testing/)

  - [编写并运行测试](/zh-hans/4.2/topics/testing/overview/)
  - [测试工具](/zh-hans/4.2/topics/testing/tools/)
  - [进阶测试主题](/zh-hans/4.2/topics/testing/advanced/)
- [Django中的用户认证](/zh-hans/4.2/topics/auth/)

  - [概况](/zh-hans/4.2/topics/auth/#overview)
  - [安装](/zh-hans/4.2/topics/auth/#installation)
  - [用法](/zh-hans/4.2/topics/auth/#usage)
- [Django 缓存框架](/zh-hans/4.2/topics/cache/)

  - [设置缓存](/zh-hans/4.2/topics/cache/#setting-up-the-cache)
  - [站点缓存](/zh-hans/4.2/topics/cache/#the-per-site-cache)
  - [视图缓存](/zh-hans/4.2/topics/cache/#the-per-view-cache)
  - [模板片段缓存](/zh-hans/4.2/topics/cache/#template-fragment-caching)
  - [底层缓存 API](/zh-hans/4.2/topics/cache/#the-low-level-cache-api)
  - [异步支持](/zh-hans/4.2/topics/cache/#asynchronous-support)
  - [下游缓存](/zh-hans/4.2/topics/cache/#downstream-caches)
  - [使用 `Vary` 标头](/zh-hans/4.2/topics/cache/#using-vary-headers)
  - [使用其他标头控制高速缓存](/zh-hans/4.2/topics/cache/#controlling-cache-using-other-headers)
  - [`MIDDLEWARE` 顺序](/zh-hans/4.2/topics/cache/#order-of-middleware)
- [条件视图处理](/zh-hans/4.2/topics/conditional-view-processing/)

  - [条件装饰器](/zh-hans/4.2/topics/conditional-view-processing/#the-condition-decorator)
  - [仅用于计算一个值的快捷方式](/zh-hans/4.2/topics/conditional-view-processing/#shortcuts-for-only-computing-one-value)
  - [将装饰器和其他 HTTP 方法一起使用](/zh-hans/4.2/topics/conditional-view-processing/#using-the-decorators-with-other-http-methods)
  - [对比中间件的条件处理](/zh-hans/4.2/topics/conditional-view-processing/#comparison-with-middleware-conditional-processing)
- [加密签名](/zh-hans/4.2/topics/signing/)

  - [保护 `SECRET_KEY` 和 `SECRET_KEY_FALLBACKS`](/zh-hans/4.2/topics/signing/#protecting-secret-key-and-secret-key-fallbacks)
  - [使用低级 API](/zh-hans/4.2/topics/signing/#using-the-low-level-api)
- [发送邮件](/zh-hans/4.2/topics/email/)

  - [快速上手](/zh-hans/4.2/topics/email/#quick-example)
  - [`send_mail()`](/zh-hans/4.2/topics/email/#send-mail)
  - [`send_mass_mail()`](/zh-hans/4.2/topics/email/#send-mass-mail)
  - [`mail_admins()`](/zh-hans/4.2/topics/email/#mail-admins)
  - [`mail_managers()`](/zh-hans/4.2/topics/email/#mail-managers)
  - [示例](/zh-hans/4.2/topics/email/#examples)
  - [防止头注入](/zh-hans/4.2/topics/email/#preventing-header-injection)
  - [`EmailMessage` 类](/zh-hans/4.2/topics/email/#the-emailmessage-class)
  - [邮件后端](/zh-hans/4.2/topics/email/#email-backends)
  - [为了开发配置邮件](/zh-hans/4.2/topics/email/#configuring-email-for-development)
- [国际化和本地化](/zh-hans/4.2/topics/i18n/)

  - [概况](/zh-hans/4.2/topics/i18n/#overview)
  - [定义](/zh-hans/4.2/topics/i18n/#definitions)
- [日志](/zh-hans/4.2/topics/logging/)

  - [概况](/zh-hans/4.2/topics/logging/#overview)
  - [安全性考虑](/zh-hans/4.2/topics/logging/#security-implications)
  - [日志模块的配置](/zh-hans/4.2/topics/logging/#configuring-logging)
- [分页](/zh-hans/4.2/topics/pagination/)

  - [`Paginator` 类](/zh-hans/4.2/topics/pagination/#the-paginator-class)
  - [例如](/zh-hans/4.2/topics/pagination/#example)
  - [`ListView` 分页](/zh-hans/4.2/topics/pagination/#paginating-a-listview)
  - [在视图函数中使用 `Paginator`](/zh-hans/4.2/topics/pagination/#using-paginator-in-a-view-function)
- [Django 的安全性](/zh-hans/4.2/topics/security/)

  - [防御跨站脚本攻击（XSS）](/zh-hans/4.2/topics/security/#cross-site-scripting-xss-protection)
  - [防御跨站点请求伪造（CSRF）](/zh-hans/4.2/topics/security/#cross-site-request-forgery-csrf-protection)
  - [防御 SQL 注入](/zh-hans/4.2/topics/security/#sql-injection-protection)
  - [防御访问劫持](/zh-hans/4.2/topics/security/#clickjacking-protection)
  - [SSL/HTTPS](/zh-hans/4.2/topics/security/#ssl-https)
  - [Host 头部验证](/zh-hans/4.2/topics/security/#host-header-validation)
  - [Referrer 策略](/zh-hans/4.2/topics/security/#referrer-policy)
  - [跨源弹出式窗口策略](/zh-hans/4.2/topics/security/#cross-origin-opener-policy)
  - [会话安全](/zh-hans/4.2/topics/security/#session-security)
  - [用户上传内容](/zh-hans/4.2/topics/security/#user-uploaded-content)
  - [其他安全性相关主题](/zh-hans/4.2/topics/security/#additional-security-topics)
- [性能和优化](/zh-hans/4.2/topics/performance/)

  - [介绍](/zh-hans/4.2/topics/performance/#introduction)
  - [一般方法](/zh-hans/4.2/topics/performance/#general-approaches)
  - [缓存](/zh-hans/4.2/topics/performance/#caching)
  - [理解惰性](/zh-hans/4.2/topics/performance/#understanding-laziness)
  - [数据库](/zh-hans/4.2/topics/performance/#databases)
  - [HTTP 性能](/zh-hans/4.2/topics/performance/#http-performance)
  - [模板性能](/zh-hans/4.2/topics/performance/#template-performance)
  - [使用现有软件的不同版本](/zh-hans/4.2/topics/performance/#using-different-versions-of-available-software)
- [序列化 Django 对象](/zh-hans/4.2/topics/serialization/)

  - [序列化数据](/zh-hans/4.2/topics/serialization/#serializing-data)
  - [反序列化数据](/zh-hans/4.2/topics/serialization/#deserializing-data)
  - [序列化格式](/zh-hans/4.2/topics/serialization/#serialization-formats)
  - [自然键](/zh-hans/4.2/topics/serialization/#natural-keys)
- [Django 配置](/zh-hans/4.2/topics/settings/)

  - [基础](/zh-hans/4.2/topics/settings/#the-basics)
  - [指定配置文件](/zh-hans/4.2/topics/settings/#designating-the-settings)
  - [默认配置](/zh-hans/4.2/topics/settings/#default-settings)
  - [在 Python 代码中使用 settings](/zh-hans/4.2/topics/settings/#using-settings-in-python-code)
  - [运行时更改设置](/zh-hans/4.2/topics/settings/#altering-settings-at-runtime)
  - [安全](/zh-hans/4.2/topics/settings/#security)
  - [可用的配置项](/zh-hans/4.2/topics/settings/#available-settings)
  - [创建你自己的配置文件](/zh-hans/4.2/topics/settings/#creating-your-own-settings)
  - [不设置 `DJANGO_SETTINGS_MODULE` 时使用配置](/zh-hans/4.2/topics/settings/#using-settings-without-setting-django-settings-module)
- [信号](/zh-hans/4.2/topics/signals/)

  - [监听信号](/zh-hans/4.2/topics/signals/#listening-to-signals)
  - [定义和发送信号](/zh-hans/4.2/topics/signals/#defining-and-sending-signals)
  - [断开信号](/zh-hans/4.2/topics/signals/#disconnecting-signals)
- [系统检查框架](/zh-hans/4.2/topics/checks/)

  - [编写自定义的检查](/zh-hans/4.2/topics/checks/#writing-your-own-checks)
- [扩展包](/zh-hans/4.2/topics/external-packages/)

  - [Localflavor](/zh-hans/4.2/topics/external-packages/#localflavor)
  - [Comments](/zh-hans/4.2/topics/external-packages/#comments)
  - [Formtools](/zh-hans/4.2/topics/external-packages/#formtools)
- [异步支持](/zh-hans/4.2/topics/async/)

  - [异步视图](/zh-hans/4.2/topics/async/#async-views)
  - [异步安全](/zh-hans/4.2/topics/async/#async-safety)
  - [异步适配函数](/zh-hans/4.2/topics/async/#async-adapter-functions)
