{"title":"如何安装 Django","version":"4.0","locale":"zh-hans","docname":"topics/install","url":"/zh-hans/4.0/topics/install/","canonical":"https://djangodocs.dev/zh-hans/4.0/topics/install/","summary":"本文档将帮助您使用 Django。Django is a Python web framework. See 我应该使用哪个版本的 Python 来配合 Django? for details.","html":"<h1>如何安装 Django<a class=\"heading-anchor\" href=\"#how-to-install-django\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>本文档将帮助您使用 Django。</p>\n<section id=\"install-python\">\n<h2>安装 Python<a class=\"heading-anchor\" href=\"#install-python\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django is a Python web framework. See <a class=\"reference internal\" href=\"/zh-hans/4.0/faq/install/#faq-python-version-support\"><span class=\"std std-ref\">我应该使用哪个版本的 Python 来配合 Django?</span></a> for\ndetails.</p>\n<p>可以通过 <a class=\"reference external\" href=\"https://www.python.org/downloads/\">https://www.python.org/downloads/</a> 或者操作系统的包管理工具获取最新版本的 Python。</p>\n<aside class=\"admonition-python-on-windows admonition\" role=\"note\">\n<p class=\"admonition-title\">Windows 上的 Python</p>\n<p>如果您刚刚开始学习 Django 并且使用 Windows，查看 <a class=\"reference internal\" href=\"/zh-hans/4.0/howto/windows/\"><span class=\"doc\">如何在 Windows 上安装 Django</span></a>  可能对你有帮助。</p>\n</aside>\n</section>\n<section id=\"install-apache-and-mod-wsgi\">\n<h2>安装 Apache 和 <code class=\"docutils literal notranslate\">mod_wsgi</code><a class=\"heading-anchor\" href=\"#install-apache-and-mod-wsgi\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>如果您只是想试验 Django，请跳到下一部分；Django 包含一个可用于测试的轻量级 Web 服务器，因此在准备好在生产环境中部署 Django 之前，您不需要设置 Apache。</p>\n<p>If you want to use Django on a production site, use <a class=\"reference external\" href=\"https://httpd.apache.org/\">Apache</a> with\n<a class=\"reference external\" href=\"https://modwsgi.readthedocs.io/en/develop/\">mod_wsgi</a>. mod_wsgi operates in one of two modes: embedded\nmode or daemon mode. In embedded mode, mod_wsgi is similar to\nmod_perl -- it embeds Python within Apache and loads Python code into\nmemory when the server starts. Code stays in memory throughout the\nlife of an Apache process, which leads to significant performance\ngains over other server arrangements. In daemon mode, mod_wsgi spawns\nan independent daemon process that handles requests. The daemon\nprocess can run as a different user than the web server, possibly\nleading to improved security. The daemon process can be restarted\nwithout restarting the entire Apache web server, possibly making\nrefreshing your codebase more seamless. Consult the mod_wsgi\ndocumentation to determine which mode is right for your setup. Make\nsure you have Apache installed with the mod_wsgi module activated.\nDjango will work with any version of Apache that supports mod_wsgi.</p>\n<p>若已安装 mod_wsgi 模块，请查看 <a class=\"reference internal\" href=\"/zh-hans/4.0/howto/deployment/wsgi/modwsgi/\"><span class=\"doc\">Django 如何利用 mod_wsgi 工作</span></a> 了解如何配置。</p>\n<p>如果由于某种原因你不能使用 mod_wsgi，请不要担心： Django 支持许多其他部署选项。一个是 <a class=\"reference internal\" href=\"/zh-hans/4.0/howto/deployment/wsgi/uwsgi/\"><span class=\"doc\">uWSGI</span></a> ；它和 <a class=\"reference external\" href=\"https://nginx.org/\">nginx</a> 配合使用很好。此外，Django 遵循 WSGI 规范（ <span class=\"target\" id=\"index-2\"></span><a class=\"pep reference external\" href=\"https://peps.python.org/pep-3333/\"><strong>PEP 3333</strong></a> ），允许它在各种服务器平台上运行。</p>\n</section>\n<section id=\"get-your-database-running\">\n<span id=\"database-installation\"></span><h2>运行你的数据库<a class=\"heading-anchor\" href=\"#get-your-database-running\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>如果你打算使用 Django 的数据库 API 功能，你需要确保一个数据库服务器正在运行。Django 支持许多不同的数据库服务器，官方支持 <a class=\"reference external\" href=\"https://www.postgresql.org/\">PostgreSQL</a>、<a class=\"reference external\" href=\"https://mariadb.org/\">MariaDB</a>、<a class=\"reference external\" href=\"https://www.mysql.com/\">MySQL</a>、<a class=\"reference external\" href=\"https://www.oracle.com/\">Oracle</a> 和 <a class=\"reference external\" href=\"https://www.sqlite.org/\">SQLite</a>。</p>\n<p>如果你正在开发一个小项目或不打算在生产环境中部署的东西，SQLite 通常是最好的选择，因为它不需要运行一个单独的服务器。然而，SQLite 与其他数据库有许多不同之处，所以如果你正在开发一些实质性的东西，建议使用你计划在生产中使用的同一数据库进行开发。</p>\n<p>除了官方支持的数据库，还有 <a class=\"reference internal\" href=\"/zh-hans/4.0/ref/databases/#third-party-notes\"><span class=\"std std-ref\">第三方提供的后端</span></a> 允许你在 Django 中使用其他数据库。</p>\n<p>除了数据库后端，你还要确保安装了 Python 数据库绑定。</p>\n<ul class=\"simple\">\n<li><p>如果你正在使用 PostgreSQL，你需要 <a class=\"reference external\" href=\"https://www.psycopg.org/\">psycopg2</a> 包。相关详细信息请参阅 <a class=\"reference internal\" href=\"/zh-hans/4.0/ref/databases/#postgresql-notes\"><span class=\"std std-ref\">PostgreSQL 笔记</span></a>。</p></li>\n<li><p>如果你正在使用 MySQL 或 MariaDB，则需要一个像 <code class=\"docutils literal notranslate\">mysqlclient</code> 一样的 <a class=\"reference internal\" href=\"/zh-hans/4.0/ref/databases/#mysql-db-api-drivers\"><span class=\"std std-ref\">DB API 驱动</span></a>。 详细信息参见 <a class=\"reference internal\" href=\"/zh-hans/4.0/ref/databases/#mysql-notes\"><span class=\"std std-ref\">MySQL 后端注意事项</span></a>。</p></li>\n<li><p>如果你正在使用 SQLite，则可能需要阅读 <a class=\"reference internal\" href=\"/zh-hans/4.0/ref/databases/#sqlite-notes\"><span class=\"std std-ref\">SQLite 后端笔记</span></a> 。</p></li>\n<li><p>如果你正在使用 Oracle，则需要 <a class=\"reference external\" href=\"https://oracle.github.io/python-cx_Oracle/\">cx_Oracle</a> 的副本，但请阅读 <a class=\"reference internal\" href=\"/zh-hans/4.0/ref/databases/#oracle-notes\"><span class=\"std std-ref\">针对 Oracle 后端的笔记</span></a> 以获取有关 Oracle 和 <code class=\"docutils literal notranslate\">cx_Oracle</code> 支持的版本的详细信息。</p></li>\n<li><p>如果你使用的是非官方的第三方后端，请参阅提供的文档以了解任何其他要求。</p></li>\n</ul>\n<p>如果你打算使用 Django 的 <code class=\"docutils literal notranslate\">manage.py migrate</code> 命令为你的模型自动创建数据库表（首先安装 Django 并创建项目后），你需要确保 Django 有权限在你使用的数据库中创建和修改表；如果你打算手动创建表，你可以授予 Django <code class=\"docutils literal notranslate\">SELECT</code>、<code class=\"docutils literal notranslate\">INSERT</code>、<code class=\"docutils literal notranslate\">UPDATE</code> 和 <code class=\"docutils literal notranslate\">DELETE</code> 权限。创建具有这些权限的数据库用户后，你将在项目的配置文件中指定详细信息，详细信息参见 <a class=\"reference internal\" href=\"/zh-hans/4.0/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\">DATABASES</code></a>。</p>\n<p>如果你正在使用 Django 的 <a class=\"reference internal\" href=\"/zh-hans/4.0/topics/testing/\"><span class=\"doc\">测试框架</span></a> 来测试数据库查询，Django 将需要创建测试数据库的权限。</p>\n</section>\n<section id=\"install-the-django-code\">\n<span id=\"install-django-code\"></span><h2>安装 Django 源码<a class=\"heading-anchor\" href=\"#install-the-django-code\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>安装过程可能会有些许差异，这取决于你是否在安装一个发行版——某个特定的版本，下载最新的正式发布包，或获取最新的开发版本。</p>\n<section id=\"installing-an-official-release-with-pip\">\n<span id=\"installing-official-release\"></span><h3>通过 <code class=\"docutils literal notranslate\">pip</code> 安装正式发布版本<a class=\"heading-anchor\" href=\"#installing-an-official-release-with-pip\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>以下是安装 Django 的推荐方式。</p>\n<ol class=\"arabic\">\n<li><p>安装 <a class=\"reference external\" href=\"https://pip.pypa.io/\">pip</a>。最简单的方式是使用 <a class=\"reference external\" href=\"https://pip.pypa.io/en/latest/installation/\">独立 pip 安装器</a>。若你的系统早已安装 <code class=\"docutils literal notranslate\">pip</code>，你可能需要更新它，因为它可能过期了。如果它过期了，你会知道的，因为过期的用不了。</p></li>\n<li><p>看一下 <a class=\"reference external\" href=\"https://docs.python.org/3/tutorial/venv.html\" title=\"(in Python v3.14)\"><span class=\"xref std std-doc\">venv</span></a>。这个工具提供了隔离的Python环境，比在系统内安装包更实用。它还允许在没有管理员权限的情况下安装包。<a class=\"reference internal\" href=\"/zh-hans/4.0/intro/contributing/\"><span class=\"doc\">贡献指南</span></a> 介绍了如何创建一个虚拟环境。</p></li>\n<li><p>在你已创建并激活一个虚拟环境后，输入以下命令：</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-0-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python -m pip install Django\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-0-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> py -m pip install Django\n</code></pre></div></div></div>\n</li>\n</ol>\n</section>\n<section id=\"installing-a-distribution-specific-package\">\n<span id=\"installing-distribution-package\"></span><h3>安装特定发行版<a class=\"heading-anchor\" href=\"#installing-a-distribution-specific-package\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>查看 <a class=\"reference internal\" href=\"/zh-hans/4.0/misc/distributions/\"><span class=\"doc\">特定发行版注意事项</span></a> 以查看你的平台／发行版是否提供了官方的 Django 软件包／安装程序。 发行版提供的软件包通常将允许自动安装依赖项和且易于升级； 但是，这些软件包很少包含最新版本的 Django。</p>\n</section>\n<section id=\"installing-the-development-version\">\n<span id=\"installing-development-version\"></span><h3>安装开发版本<a class=\"heading-anchor\" href=\"#installing-the-development-version\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<aside class=\"admonition-tracking-django-development admonition\" role=\"note\">\n<p class=\"admonition-title\">跟踪 Django 开发</p>\n<p>如果你决定使用 Django 的最新开发版，你需要关注 <a class=\"reference external\" href=\"https://code.djangoproject.com/timeline\">开发版时间轴</a> 和 <a class=\"reference internal\" href=\"/zh-hans/4.0/releases/#development-release-notes\"><span class=\"std std-ref\">即将到来的新特性的发布说明</span></a>。这将保证你能获取所有新特性和最新的代码。（对于稳定发布版，所有必要的修改都在发布说明中记录。）</p>\n</aside>\n<p>如果你希望偶尔能获取最新的补丁和改进，遵循以下说明：</p>\n<ol class=\"arabic\">\n<li><p>确保你已安装了 <a class=\"reference external\" href=\"https://git-scm.com/\">Git</a>，这样你就可以从 shell 运行对应命令。（在 shell 中输入 <code class=\"docutils literal notranslate\">git help</code> 测试是否安装。）</p></li>\n<li><p>像这样检出 Django 的主开发分支：</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-1-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>git clone https://github.com/django/django.git\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-1-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> git clone https://github.com/django/django.git\n</code></pre></div></div></div>\n<p>这会在当前目录创建一个 <code class=\"docutils literal notranslate\">django</code> 目录。</p>\n</li>\n<li><p>确保 Python 解释器可以加载 Django 的代码。最方便的方法是使用虚拟环境和 <a class=\"reference external\" href=\"https://pip.pypa.io/\">pip</a>。 <a class=\"reference internal\" href=\"/zh-hans/4.0/intro/contributing/\"><span class=\"doc\">贡献指南</span></a> 简略介绍了如何创建虚拟环境。</p></li>\n<li><p>设置并激活虚拟环境后，运行以下命令：</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-2-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python -m pip install -e django/\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-2-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> py -m pip install -e django\\\n</code></pre></div></div></div>\n<p>这会让 Django 的代码可导入，使得 <code class=\"docutils literal notranslate\">django-admin</code> 命令行工具可用。换句话说，大事可为。</p>\n</li>\n</ol>\n<p>当你想更新你的 Django 源代码时，在 <code class=\"docutils literal notranslate\">django</code> 目录下运行 <code class=\"docutils literal notranslate\">git pull</code> 命令。当你这样做的时候，Git 会下载所有变更。</p>\n</section>\n</section>","rootId":"how-to-install-django","toc":[{"title":"安装 Python","anchor":"install-python","children":[]},{"title":"安装 Apache 和 mod_wsgi","anchor":"install-apache-and-mod-wsgi","children":[]},{"title":"运行你的数据库","anchor":"get-your-database-running","children":[]},{"title":"安装 Django 源码","anchor":"install-the-django-code","children":[{"title":"通过 pip 安装正式发布版本","anchor":"installing-an-official-release-with-pip","children":[]},{"title":"安装特定发行版","anchor":"installing-a-distribution-specific-package","children":[]},{"title":"安装开发版本","anchor":"installing-the-development-version","children":[]}]}],"breadcrumbs":[{"docname":"topics/index","title":"使用 Django","url":"/zh-hans/4.0/topics/"}],"prev":{"docname":"topics/index","title":"使用 Django","url":"/zh-hans/4.0/topics/"},"next":{"docname":"topics/db/index","title":"模型和数据库","url":"/zh-hans/4.0/topics/db/"},"formats":{"html":"/zh-hans/4.0/topics/install/","markdown":"/zh-hans/4.0/topics/install.md","json":"/zh-hans/4.0/topics/install.json"},"source":"https://github.com/django/django/blob/stable/4.0.x/docs/topics/install.txt","official":"https://docs.djangoproject.com/zh-hans/4.0/topics/install/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}