{"title":"如何在 Windows 上安装 Django","version":"3.0","locale":"zh-hans","docname":"howto/windows","url":"/zh-hans/3.0/howto/windows/","canonical":"https://djangodocs.dev/zh-hans/3.0/howto/windows/","summary":"This document will guide you through installing Python 3.8 and Django on Windows.","html":"<h1>如何在 Windows 上安装 Django<a class=\"heading-anchor\" href=\"#how-to-install-django-on-windows\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>This document will guide you through installing Python 3.8 and Django on\nWindows. It also provides instructions for setting up a virtual environment,\nwhich makes it easier to work on Python projects. This is meant as a beginner's\nguide for users working on Django projects and does not reflect how Django\nshould be installed when developing patches for Django itself.</p>\n<p>The steps in this guide have been tested with Windows 10. In other\nversions, the steps would be similar. You will need to be familiar with using\nthe Windows command prompt.</p>\n<section id=\"install-python\">\n<span id=\"install-python-windows\"></span><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, thus requiring Python to be installed on your\nmachine. At the time of writing, Python 3.8 is the latest version.</p>\n<p>To install Python on your machine go to <a class=\"reference external\" href=\"https://python.org/downloads/\">https://python.org/downloads/</a>. The\nwebsite should offer you a download button for the latest Python version.\nDownload the executable installer and run it. Check the boxes next to &quot;Install\nlauncher for all users (recommended)&quot; then click &quot;Install Now&quot;.</p>\n<p>安装后，打开命令提示符，检查 Python 版本是否与你装的一致，通过运行:</p>\n<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 code\"><code><span class=\"gp\">...\\&gt;</span> py --version\n</code></pre></div>\n<aside class=\"admonition admonition-seealso\" role=\"note\">\n<p class=\"admonition-title\">See also</p>\n<p>想知晓更多细节，请查看 <a class=\"reference external\" href=\"https://docs.python.org/3/using/windows.html\" title=\"(in Python v3.14)\"><span>Using Python on Windows</span></a> 文档。</p>\n</aside>\n</section>\n<section id=\"about-pip\">\n<h2>关于 <code class=\"docutils literal notranslate\">pip</code><a class=\"heading-anchor\" href=\"#about-pip\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p><a class=\"reference external\" href=\"https://pypi.org/project/pip/\">pip</a> is a package manager for Python and is included by default with the\nPython installer. It helps to install and uninstall Python packages\n(such as Django!). For the rest of the installation, we'll use <code class=\"docutils literal notranslate\">pip</code> to\ninstall Python packages from the command line.</p>\n</section>\n<section id=\"setting-up-a-virtual-environment\">\n<span id=\"virtualenvironment\"></span><h2>Setting up a virtual environment<a class=\"heading-anchor\" href=\"#setting-up-a-virtual-environment\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>It is best practice to provide a dedicated environment for each Django project\nyou create. There are many options to manage environments and packages within\nthe Python ecosystem, some of which are recommended in the <a class=\"reference external\" href=\"https://packaging.python.org/guides/tool-recommendations/\">Python\ndocumentation</a>.\nPython itself comes with <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> for managing\nenvironments which we will use for this guide.</p>\n<p>To create a virtual environment for your project, open a new command prompt,\nnavigate to the folder where you want to create your project and then enter the\nfollowing:</p>\n<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 code\"><code><span class=\"gp\">...\\&gt;</span> py -m venv project-name\n</code></pre></div>\n<p>This will create a folder called 'project-name' if it does not already exist\nand setup the virtual environment. To activate the environment, run:</p>\n<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 code\"><code><span class=\"gp\">...\\&gt;</span> project-name\\Scripts\\activate.bat\n</code></pre></div>\n<p>The virtual environment will be activated and you'll see &quot;(project-name)&quot; next\nto the command prompt to designate that. Each time you start a new command\nprompt, you'll need to activate the environment again.</p>\n</section>\n<section id=\"install-django\">\n<h2>安装 Django<a class=\"heading-anchor\" href=\"#install-django\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django 可以轻松地在你的虚拟环境中使用 <code class=\"docutils literal notranslate\">pip</code> 安装。</p>\n<p>在命令提示行中，确认虚拟环境是激活的，然后运行以下命令:</p>\n<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 code\"><code><span class=\"gp\">...\\&gt;</span> py -m pip install Django\n</code></pre></div>\n<p>这将下载并安装最新的 Django 发布版本。</p>\n<p>安装完成后，你可以在命令提示符运行 <code class=\"docutils literal notranslate\">django-admin --version</code> 验证你安装的 Django。</p>\n<p>参考 <span class=\"xref std std-ref\">数据库安装</span> 了解如何通过 Django 安装数据库。</p>\n</section>\n<section id=\"common-pitfalls\">\n<h2>常见失误<a class=\"heading-anchor\" href=\"#common-pitfalls\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<ul>\n<li><p>若 <code class=\"docutils literal notranslate\">django-admin</code> 无论输入啥参数都只显示帮助信息，这可能是一个 Windos 的文件关联问题。检查 <code class=\"docutils literal notranslate\">PATH</code> 中是否不止一个用于运行 Python 脚本的环境变量。该问题通常在安装了多个 Python 版本时出现。</p></li>\n<li><p>若你通过代理来连接互联网，运行命令 <code class=\"docutils literal notranslate\">pip install Django</code> 时可能会出现问题。在命令提示符中为代理配置如下环境变量:</p>\n<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 code\"><code><span class=\"gp\">...\\&gt;</span> <span class=\"k\">set</span> <span class=\"nv\">http_proxy</span><span class=\"p\">=</span>http://username:password@proxyserver:proxyport\n<span class=\"gp\">...\\&gt;</span> <span class=\"k\">set</span> <span class=\"nv\">https_proxy</span><span class=\"p\">=</span>https://username:password@proxyserver:proxyport\n</code></pre></div>\n</li>\n</ul>\n</section>","rootId":"how-to-install-django-on-windows","toc":[{"title":"安装Python","anchor":"install-python","children":[]},{"title":"关于 pip","anchor":"about-pip","children":[]},{"title":"Setting up a virtual environment","anchor":"setting-up-a-virtual-environment","children":[]},{"title":"安装 Django","anchor":"install-django","children":[]},{"title":"常见失误","anchor":"common-pitfalls","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"操作指南","url":"/zh-hans/3.0/howto/"}],"prev":{"docname":"howto/static-files/index","title":"管理静态文件（比如图片、JavaScript、CSS）","url":"/zh-hans/3.0/howto/static-files/"},"next":{"docname":"howto/writing-migrations","title":"编写数据库迁移语句","url":"/zh-hans/3.0/howto/writing-migrations/"},"formats":{"html":"/zh-hans/3.0/howto/windows/","markdown":"/zh-hans/3.0/howto/windows.md","json":"/zh-hans/3.0/howto/windows.json"},"source":"https://github.com/django/django/blob/stable/3.0.x/docs/howto/windows.txt","official":"https://docs.djangoproject.com/zh-hans/3.0/howto/windows/","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"]}