如何在Windows上安装Django
This document will guide you through installing Python 3.5 and Django onWindows. It also provides instructions for installing virtualenv andvirtualenvwrapper, which make it easier to work on Python projects. This ismeant as a beginner's guide for users working on Django projects and does notreflect how Django should be installed when developing patches for Djangoitself.
The steps in this guide have been tested with Windows 7, 8, and 10. In otherversions, the steps would be similar. You will need to be familiar with usingthe Windows command prompt.
安装Python
Django 是一个 Python Web 框架,因此需要在您的机器上安装 Python。在本文撰写时,Python 最新的版本是 3.5。
为了把 Python 安装到你的机器上,请打开https://python.org/downloads/。这个网站应该为你提供了一个最新的 Python 版本的下载按钮。下载可执行安装包并且运行它。运行后,选择把 Python 3.5 添加到环境变量
,然后点击马上安装
。
After installation, open the command prompt and check that the Python versionmatches the version you installed by executing:
- python --version
关于 pip
pip is a package manage for Python. It makes installing and uninstallingPython packages (such as Django!) very easy. For the rest of the installation,we'll use pip
to install Python packages from the command line.
想要在您的机器上安装 pip,请转至 https://pip.pypa.io/en/latest/installing/,并按照“Installing with get-pip.py”说明进行操作
安装 virtualenv 和 virtualenvwrapper
virtualenv and virtualenvwrapper provide a dedicated environment foreach Django project you create. While not mandatory, this is considered a bestpractice and will save you time in the future when you're ready to deploy yourproject. Simply type:
- pip install virtualenvwrapper-win
然后为您的项目创建一个虚拟环境:
- mkvirtualenv myproject
The virtual environment will be activated automatically and you'll see"(myproject)" next to the command prompt to designate that. If you start a newcommand prompt, you'll need to activate the environment again using:
- workon myproject
安装Django
Django可以轻松地在你的虚拟环境中使用pip
安装
In the command prompt, ensure your virtual environment is active, and executethe following command:
- pip install django
这将下载并安装最新的Django版本。
After the installation has completed, you can verify your Django installationby executing django-admin —version
in the command prompt.
See 运行你的数据库 for information on database installationwith Django.
Common pitfalls
If
django-admin
only displays the help text no matter what argumentsit is given, there is probably a problem with the file association inWindows. Check if there is more than one environment variable set forrunning Python scripts inPATH
. This usually occurs when there is morethan one Python version installed.If you are connecting to the internet behind a proxy, there might be problemin running the command
pip install django
. Set the environment variablesfor proxy configuration in the command prompt as follows:
- set http_proxy=http://username:password@proxyserver:proxyport
- set https_proxy=https://username:password@proxyserver:proxyport