{"title":"如何安装Django","version":"2.1","locale":"zh-hans","docname":"topics/install","url":"/zh-hans/2.1/topics/install/","canonical":"https://djangodocs.dev/zh-hans/2.1/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/2.1/faq/install/#faq-python-version-support\"><span class=\"std std-ref\">我应该使用哪个版本的 Python 来配合 Django?</span></a> for\ndetails.</p>\n<p>获取最新版本的Python可以通过：访问 <a class=\"reference external\" href=\"https://www.python.org/downloads/\">https://www.python.org/downloads/</a> ；或者操作系统的包管理工具。</p>\n<aside class=\"admonition-django-on-jython admonition\" role=\"note\">\n<p class=\"admonition-title\">基于 Jython 的 Django</p>\n<p><a class=\"reference external\" href=\"http://www.jython.org/\">Jython</a> （一个针对Java平台的Python实现）和Python 3不兼容，所以Django 2.0及以上的版本无法在Jython上运行。</p>\n</aside>\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/2.1/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>如果要在生产站点上使用Django，请将 <a class=\"reference external\" href=\"https://httpd.apache.org/\">Apache</a> 与 <a class=\"reference external\" href=\"https://modwsgi.readthedocs.io/en/develop/\">mod_wsgi</a> 一起使用。 mod_wsgi以两种模式中的一种运行：嵌入模式或守护进程模式。在嵌入模式下，mod_wsgi类似于mod_perl —— 它在Apache中嵌入Python，并在服务器启动时将Python代码加载到内存中。代码在Apache进程的整个生命周期中都保留在内存中，与其他服务器相比，这可以显着提高性能。在守护进程模式下，mod_wsgi 会生成一个处理请求的独立守护进程。守护进程可以作为与Web服务器不同的用户运行，可能会提高安全性。可以在不重新启动整个Apache Web服务器的情况下重新启动守护进程，从而可以更加无缝地刷新代码库。请参阅mod_wsgi文档以确定适合您的设置的模式。确保你在安装了Apache并且启用了mod_wsgi模块。 Django将在任何支持mod_wsgi的Apache版本上工作。</p>\n<p>关于如何在安装后配置mod_wsgi模块，请查看 <a class=\"reference internal\" href=\"/zh-hans/2.1/howto/deployment/wsgi/modwsgi/\"><span class=\"doc\">How to use Django with mod_wsgi</span></a> 。</p>\n<p>如果由于某种原因你不能使用mod_wsgi，请不要担心： Django支持许多其他部署选项。一个是 <a class=\"reference internal\" href=\"/zh-hans/2.1/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://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/2.1/ref/databases/#third-party-notes\"><span class=\"std std-ref\">backends provided by 3rd parties</span></a> 允许你在Django中使用其他数据库。</p>\n<p>除了数据库后端，你还要确保安装了Python数据库绑定。</p>\n<ul class=\"simple\">\n<li><p>如果你正在使用PostgreSQL，你需要 <a class=\"reference external\" href=\"http://initd.org/psycopg/\">psycopg2</a> 包。相关详细信息请参阅 <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/databases/#postgresql-notes\"><span class=\"std std-ref\">PostgreSQL notes</span></a> 。</p></li>\n<li><p>如果你正在使用MySQL，则需要一个像 <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>mysqlclient``这样的 <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/databases/#mysql-db-api-drivers\"><span class=\"std std-ref\">DB API driver</span></a> 。相关详细信息请参阅 <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/databases/#mysql-notes\"><span class=\"std std-ref\">notes for the MySQL backend</span></a> 。</p></li>\n<li><p>如果你正在使用SQLite，则可能需要阅读 <a class=\"reference internal\" href=\"/zh-hans/2.1/ref/databases/#sqlite-notes\"><span class=\"std std-ref\">SQLite backend notes</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/2.1/ref/databases/#oracle-notes\"><span class=\"std std-ref\">notes for the Oracle backend</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> 和 <a href=\"#id1\"><span class=\"problematic\" id=\"id2\">``</span></a>DELETE``权限。创建具有这些权限的数据库用户后，您要在项目的配置文件中指定详细信息，请参阅 <a class=\"reference internal\" href=\"/zh-hans/2.1/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/2.1/topics/testing/\"><span class=\"doc\">testing framework</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>Installation instructions are slightly different depending on whether you're\ninstalling a distribution-specific package, downloading the latest official\nrelease, or fetching the latest development version.</p>\n<p>It's easy, no matter which way you choose.</p>\n<section id=\"installing-an-official-release-with-pip\">\n<span id=\"installing-official-release\"></span><h3>Installing an official release with <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>This is the recommended way to install Django.</p>\n<ol class=\"arabic\">\n<li><p>Install <a class=\"reference external\" href=\"https://pip.pypa.io/\">pip</a>. The easiest is to use the <a class=\"reference external\" href=\"https://pip.pypa.io/en/latest/installing/#installing-with-get-pip-py\">standalone pip installer</a>. If your\ndistribution already has <code class=\"docutils literal notranslate\">pip</code> installed, you might need to update it if\nit's outdated. If it's outdated, you'll know because installation won't\nwork.</p></li>\n<li><p>Take a look at <a class=\"reference external\" href=\"https://virtualenv.pypa.io/\">virtualenv</a> and <a class=\"reference external\" href=\"https://virtualenvwrapper.readthedocs.io/en/latest/\">virtualenvwrapper</a>. These tools provide\nisolated Python environments, which are more practical than installing\npackages systemwide. They also allow installing packages without\nadministrator privileges. The <a class=\"reference internal\" href=\"/zh-hans/2.1/intro/contributing/\"><span class=\"doc\">contributing tutorial</span></a> walks through how to create a virtualenv.</p></li>\n<li><p>After you've created and activated a virtual environment, enter the command:</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>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> 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>Installing a distribution-specific package<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>Check the <a class=\"reference internal\" href=\"/zh-hans/2.1/misc/distributions/\"><span class=\"doc\">distribution specific notes</span></a> to see if\nyour platform/distribution provides official Django packages/installers.\nDistribution-provided packages will typically allow for automatic installation\nof dependencies and easy upgrade paths; however, these packages will rarely\ncontain the latest release of Django.</p>\n</section>\n<section id=\"installing-the-development-version\">\n<span id=\"installing-development-version\"></span><h3>Installing the development version<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\">Tracking Django development</p>\n<p>If you decide to use the latest development version of Django,\nyou'll want to pay close attention to <a class=\"reference external\" href=\"https://code.djangoproject.com/timeline\">the development timeline</a>,\nand you'll want to keep an eye on the <a class=\"reference internal\" href=\"/zh-hans/2.1/releases/#development-release-notes\"><span class=\"std std-ref\">release notes for the\nupcoming release</span></a>. This will help you stay\non top of any new features you might want to use, as well as any changes\nyou'll need to make to your code when updating your copy of Django.\n(For stable releases, any necessary changes are documented in the\nrelease notes.)</p>\n</aside>\n<p>If you'd like to be able to update your Django code occasionally with the\nlatest bug fixes and improvements, follow these instructions:</p>\n<ol class=\"arabic\">\n<li><p>Make sure that you have <a class=\"reference external\" href=\"https://git-scm.com/\">Git</a> installed and that you can run its commands\nfrom a shell. (Enter <code class=\"docutils literal notranslate\">git help</code> at a shell prompt to test this.)</p></li>\n<li><p>Check out Django's main development branch like so:</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>This will create a directory <code class=\"docutils literal notranslate\">django</code> in your current directory.</p>\n</li>\n<li><p>Make sure that the Python interpreter can load Django's code. The most\nconvenient way to do this is to use <a class=\"reference external\" href=\"https://virtualenv.pypa.io/\">virtualenv</a>, <a class=\"reference external\" href=\"https://virtualenvwrapper.readthedocs.io/en/latest/\">virtualenvwrapper</a>, and\n<a class=\"reference external\" href=\"https://pip.pypa.io/\">pip</a>. The <a class=\"reference internal\" href=\"/zh-hans/2.1/intro/contributing/\"><span class=\"doc\">contributing tutorial</span></a> walks through\nhow to create a virtualenv.</p></li>\n<li><p>After setting up and activating the virtualenv, run the following command:</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>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> pip install -e django\\\n</code></pre></div></div></div>\n<p>This will make Django's code importable, and will also make the\n<code class=\"docutils literal notranslate\">django-admin</code> utility command available. In other words, you're all\nset!</p>\n</li>\n</ol>\n<p>When you want to update your copy of the Django source code, just run the\ncommand <code class=\"docutils literal notranslate\">git pull</code> from within the <code class=\"docutils literal notranslate\">django</code> directory. When you do this,\nGit will automatically download any changes.</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":"Installing an official release with pip","anchor":"installing-an-official-release-with-pip","children":[]},{"title":"Installing a distribution-specific package","anchor":"installing-a-distribution-specific-package","children":[]},{"title":"Installing the development version","anchor":"installing-the-development-version","children":[]}]}],"breadcrumbs":[{"docname":"topics/index","title":"Using Django","url":"/zh-hans/2.1/topics/"}],"prev":{"docname":"topics/index","title":"Using Django","url":"/zh-hans/2.1/topics/"},"next":{"docname":"topics/db/index","title":"模型和数据库","url":"/zh-hans/2.1/topics/db/"},"formats":{"html":"/zh-hans/2.1/topics/install/","markdown":"/zh-hans/2.1/topics/install.md","json":"/zh-hans/2.1/topics/install.json"},"source":"https://github.com/django/django/blob/stable/2.1.x/docs/topics/install.txt","official":"https://docs.djangoproject.com/zh-hans/2.1/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","pt-br","ko","es","el","pl"]}