“Django/docs/3.0.x/howto/windows”的版本间差异

来自菜鸟教程
Django/docs/3.0.x/howto/windows
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:如何在 Windows 上安装 Django — Django 文档}}
 
<div id="how-to-install-django-on-windows" class="section">
 
<div id="how-to-install-django-on-windows" class="section">
  
 
= 如何在 Windows 上安装 Django =
 
= 如何在 Windows 上安装 Django =
  
This document will guide you through installing Python 3.8 and Django on
+
本文档将指导您在 Windows 上安装 Python 3.8 和 Django。 它还提供了设置虚拟环境的说明,这使得在 Python 项目上工作变得更加容易。 这是为从事 Django 项目的用户提供的初学者指南,并不反映在为 Django 本身开发补丁时应如何安装 Django。
Windows. It also provides instructions for setting up a virtual environment,
 
which makes it easier to work on Python projects. This is meant as a beginner's
 
guide for users working on Django projects and does not reflect how Django
 
should be installed when developing patches for Django itself.
 
  
The steps in this guide have been tested with Windows 10. In other
+
本指南中的步骤已经过 Windows 10 测试。 在其他版本中,步骤类似。 您需要熟悉使用 Windows 命令提示符。
versions, the steps would be similar. You will need to be familiar with using
 
the Windows command prompt.
 
  
 
<div id="install-python" class="section">
 
<div id="install-python" class="section">
  
 
<span id="install-python-windows"></span>
 
<span id="install-python-windows"></span>
== 安装Python ==
+
== 安装 Python ==
  
Django is a Python web framework, thus requiring Python to be installed on your
+
Django 是一个 Python Web 框架,因此需要在您的机器上安装 Python。 在撰写本文时,Python 3.8 是最新版本。
machine. At the time of writing, Python 3.8 is the latest version.
 
  
To install Python on your machine go to https://python.org/downloads/. The
+
要在您的机器上安装 Python,请访问 https://python.org/downloads/。 该网站应该为您提供最新 Python 版本的下载按钮。 下载可执行安装程序并运行它。 选中“为所有用户安装启动器(推荐)”旁边的框,然后单击“立即安装”。
website should offer you a download button for the latest Python version.
 
Download the executable installer and run it. Check the boxes next to &quot;Install
 
launcher for all users (recommended)&quot; then click &quot;Install Now&quot;.
 
  
安装后,打开命令提示符,检查 Python 版本是否与你装的一致,通过运行:
+
安装后,打开命令提示符并通过执行以下命令检查 Python 版本是否与您安装的版本匹配:
  
 
<div class="highlight-doscon notranslate">
 
<div class="highlight-doscon notranslate">
第32行: 第23行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>...\&gt; py --version</pre>
+
<syntaxhighlight lang="doscon">...\> py --version</syntaxhighlight>
  
 
</div>
 
</div>
第39行: 第30行:
 
<div class="admonition seealso">
 
<div class="admonition seealso">
  
参见
+
也可以看看
  
想知晓更多细节,请查看 <span class="xref std std-doc">python:using/windows</span> 文档。
+
有关更多详细信息,请参阅 <span class="xref std std-doc">python:using/windows</span> 文档。
  
  
第49行: 第40行:
 
<div id="about-pip" class="section">
 
<div id="about-pip" class="section">
  
== 关于 <code>pip</code> ==
+
== 关于pip ==
  
[https://pypi.org/project/pip/ pip] is a package manager for Python and is included by default with the
+
[https://pypi.org/project/pip/ pip] Python 的包管理器,默认包含在 Python 安装程序中。 它有助于安装和卸载 Python 包(例如 Django!)。 对于安装的其余部分,我们将使用 <code>pip</code> 从命令行安装 Python 包。
Python installer. It helps to install and uninstall Python packages
 
(such as Django!). For the rest of the installation, we'll use <code>pip</code> to
 
install Python packages from the command line.
 
  
  
第61行: 第49行:
  
 
<span id="virtualenvironment"></span>
 
<span id="virtualenvironment"></span>
== Setting up a virtual environment ==
+
== 设置虚拟环境 ==
  
It is best practice to provide a dedicated environment for each Django project
+
最佳实践是为您创建的每个 Django 项目提供一个专用环境。 在 Python 生态系统中有许多管理环境和包的选项,其中一些在 [https://packaging.python.org/guides/tool-recommendations/ Python 文档] 中有推荐。 Python 本身带有 <span class="xref std std-doc">venv</span> 用于管理我们将在本指南中使用的环境。
you create. There are many options to manage environments and packages within
 
the Python ecosystem, some of which are recommended in the [https://packaging.python.org/guides/tool-recommendations/ Python
 
documentation].
 
Python itself comes with <span class="xref std std-doc">venv</span> for managing
 
environments which we will use for this guide.
 
  
To create a virtual environment for your project, open a new command prompt,
+
要为您的项目创建虚拟环境,请打开一个新的命令提示符,导航到您要创建项目的文件夹,然后输入以下内容:
navigate to the folder where you want to create your project and then enter the
 
following:
 
  
 
<div class="highlight-doscon notranslate">
 
<div class="highlight-doscon notranslate">
第78行: 第59行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>...\&gt; py -m venv project-name</pre>
+
<syntaxhighlight lang="doscon">...\> py -m venv project-name</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
This will create a folder called 'project-name' if it does not already exist
+
如果它不存在并设置虚拟环境,这将创建一个名为“project-name”的文件夹。 要激活环境,请运行:
and setup the virtual environment. To activate the environment, run:
 
  
 
<div class="highlight-doscon notranslate">
 
<div class="highlight-doscon notranslate">
第90行: 第70行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>...\&gt; project-name\Scripts\activate.bat</pre>
+
<syntaxhighlight lang="doscon">...\> project-name\Scripts\activate.bat</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
The virtual environment will be activated and you'll see &quot;(project-name)&quot; next
+
虚拟环境将被激活,您将在命令提示符旁边看到“(项目名称)”来指定它。 每次启动新的命令提示符时,都需要再次激活环境。
to the command prompt to designate that. Each time you start a new command
 
prompt, you'll need to activate the environment again.
 
  
  
第105行: 第83行:
 
== 安装 Django ==
 
== 安装 Django ==
  
Django 可以轻松地在你的虚拟环境中使用 <code>pip</code> 安装。
+
可以在您的虚拟环境中使用 <code>pip</code> 轻松安装 Django。
  
在命令提示行中,确认虚拟环境是激活的,然后运行以下命令:
+
在命令提示符中,确保您的虚拟环境处于活动状态,然后执行以下命令:
  
 
<div class="highlight-doscon notranslate">
 
<div class="highlight-doscon notranslate">
第113行: 第91行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>...\&gt; py -m pip install Django</pre>
+
<syntaxhighlight lang="doscon">...\> py -m pip install Django</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
这将下载并安装最新的 Django 发布版本。
+
这将下载并安装最新的 Django 版本。
  
安装完成后,你可以在命令提示符运行 <code>django-admin --version</code> 验证你安装的 Django。
+
安装完成后,您可以通过在命令提示符下执行 <code>django-admin --version</code> 来验证您的 Django 安装。
  
参考 <span class="xref std std-ref">数据库安装</span> 了解如何通过 Django 安装数据库。
+
有关使用 Django 安装数据库的信息,请参阅 [[../../topics/install#database-installation|使您的数据库运行]] 。
  
  
第128行: 第106行:
 
<div id="common-pitfalls" class="section">
 
<div id="common-pitfalls" class="section">
  
== 常见失误 ==
+
== 常见的陷阱 ==
  
 
<ul>
 
<ul>
<li><p><code>django-admin</code> 无论输入啥参数都只显示帮助信息,这可能是一个 Windos 的文件关联问题。检查 <code>PATH</code> 中是否不止一个用于运行 Python 脚本的环境变量。该问题通常在安装了多个 Python 版本时出现。</p></li>
+
<li><p>如果 <code>django-admin</code> 无论给出什么参数都只显示帮助文本,则 Windows 中的文件关联可能有问题。 检查 <code>PATH</code> 中是否设置了多个用于运行 Python 脚本的环境变量。 这通常发生在安装了多个 Python 版本时。</p></li>
<li><p>若你通过代理来连接互联网,运行命令 <code>pip install Django</code> 时可能会出现问题。在命令提示符中为代理配置如下环境变量:</p>
+
<li><p>如果您通过代理连接到 Internet,则在运行命令 <code>py -m pip install Django</code> 时可能会出现问题。 在命令提示符中设置代理配置的环境变量如下:</p>
 
<div class="highlight-doscon notranslate">
 
<div class="highlight-doscon notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>...\&gt; set http_proxy=http://username:password@proxyserver:proxyport
+
<syntaxhighlight lang="doscon">...\> set http_proxy=http://username:password@proxyserver:proxyport
...\&gt; set https_proxy=https://username:password@proxyserver:proxyport</pre>
+
...\> set https_proxy=https://username:password@proxyserver:proxyport</syntaxhighlight>
  
 
</div>
 
</div>
第148行: 第126行:
  
 
</div>
 
</div>
 +
<div class="clearer">
  
[[Category:Django 3.0.x 中文文档]]
+
 
 +
 
 +
</div>
 +
 
 +
[[Category:Django 3.0.x 文档]]

2021年10月31日 (日) 04:08的最新版本

如何在 Windows 上安装 Django

本文档将指导您在 Windows 上安装 Python 3.8 和 Django。 它还提供了设置虚拟环境的说明,这使得在 Python 项目上工作变得更加容易。 这是为从事 Django 项目的用户提供的初学者指南,并不反映在为 Django 本身开发补丁时应如何安装 Django。

本指南中的步骤已经过 Windows 10 测试。 在其他版本中,步骤类似。 您需要熟悉使用 Windows 命令提示符。

安装 Python

Django 是一个 Python Web 框架,因此需要在您的机器上安装 Python。 在撰写本文时,Python 3.8 是最新版本。

要在您的机器上安装 Python,请访问 https://python.org/downloads/。 该网站应该为您提供最新 Python 版本的下载按钮。 下载可执行安装程序并运行它。 选中“为所有用户安装启动器(推荐)”旁边的框,然后单击“立即安装”。

安装后,打开命令提示符并通过执行以下命令检查 Python 版本是否与您安装的版本匹配:

...\> py --version

也可以看看

有关更多详细信息,请参阅 python:using/windows 文档。


关于pip

pip 是 Python 的包管理器,默认包含在 Python 安装程序中。 它有助于安装和卸载 Python 包(例如 Django!)。 对于安装的其余部分,我们将使用 pip 从命令行安装 Python 包。


设置虚拟环境

最佳实践是为您创建的每个 Django 项目提供一个专用环境。 在 Python 生态系统中有许多管理环境和包的选项,其中一些在 Python 文档 中有推荐。 Python 本身带有 venv 用于管理我们将在本指南中使用的环境。

要为您的项目创建虚拟环境,请打开一个新的命令提示符,导航到您要创建项目的文件夹,然后输入以下内容:

...\> py -m venv project-name

如果它不存在并设置虚拟环境,这将创建一个名为“project-name”的文件夹。 要激活环境,请运行:

...\> project-name\Scripts\activate.bat

虚拟环境将被激活,您将在命令提示符旁边看到“(项目名称)”来指定它。 每次启动新的命令提示符时,都需要再次激活环境。


安装 Django

可以在您的虚拟环境中使用 pip 轻松安装 Django。

在命令提示符中,确保您的虚拟环境处于活动状态,然后执行以下命令:

...\> py -m pip install Django

这将下载并安装最新的 Django 版本。

安装完成后,您可以通过在命令提示符下执行 django-admin --version 来验证您的 Django 安装。

有关使用 Django 安装数据库的信息,请参阅 使您的数据库运行


常见的陷阱

  • 如果 django-admin 无论给出什么参数都只显示帮助文本,则 Windows 中的文件关联可能有问题。 检查 PATH 中是否设置了多个用于运行 Python 脚本的环境变量。 这通常发生在安装了多个 Python 版本时。

  • 如果您通过代理连接到 Internet,则在运行命令 py -m pip install Django 时可能会出现问题。 在命令提示符中设置代理配置的环境变量如下:

    ...\> set http_proxy=http://username:password@proxyserver:proxyport
    ...\> set https_proxy=https://username:password@proxyserver:proxyport