Django 简明教程

Django - Environment

Django 开发环境包括安装和设置 Python、Django 和数据库系统。由于 Django 涉及到 Web 应用程序,因此值得一提的是,你还需要设置一个 Web 服务器。

Django development environment consists of installing and setting up Python, Django, and a Database System. Since Django deals with web application, it’s worth mentioning that you would need a web server setup as well.

Step 1 – Installing Python

Django 使用 100% 纯 Python 代码编写,因此你需要在系统上安装 Python。最新版本的 Django 需要 Python 2.6.5 或更高版本。

Django is written in 100% pure Python code, so you’ll need to install Python on your system. Latest Django version requires Python 2.6.5 or higher

如果你的电脑使用最新的 Linux 或 Mac OS X 发行版,那你可能已经安装了 Python。你可以通过在命令提示符中键入 python 命令来验证这一点。如果你看到类似这样的内容,则表明已经安装了 Python。

If you’re on one of the latest Linux or Mac OS X distribution, you probably already have Python installed. You can verify it by typing python command at a command prompt. If you see something like this, then Python is installed.

$ python
Python 2.7.5 (default, Jun 17 2014, 18:11:42)
[GCC 4.8.2 20140120 (Red Hat 4.8.2-16)] on linux2

否则,你可以在 http://www.python.org/download 链接中下载并安装最新版本的 Python。

Otherwise, you can download and install the latest version of Python from the link http://www.python.org/download.

Step 2 - Installing Django

安装 Django 非常容易,但所需的安装步骤取决于你的操作系统。由于 Python 是一个平台无关的语言,因此 Django 有适用于所有操作系统的通用软件包。

Installing Django is very easy, but the steps required for its installation depends on your operating system. Since Python is a platform-independent language, Django has one package that works everywhere regardless of your operating system.

你可以从 http://www.djangoproject.com/download 链接中下载最新版本的 Django。

You can download the latest version of Django from the link http://www.djangoproject.com/download.

UNIX/Linux and Mac OS X Installation

如果你运行的是 Linux 或 Mac OS 系统,则有两种安装 Django 的方法——

You have two ways of installing Django if you are running Linux or Mac OS system −

  1. You can use the package manager of your OS, or use easy_install or pip if installed.

  2. Install it manually using the official archive you downloaded before.

我们将介绍第二种方案,因为第一种方案取决于你的操作系统发行版。如果你决定按照第一种方案进行操作,请小心你安装的 Django 版本。

We will cover the second option as the first one depends on your OS distribution. If you have decided to follow the first option, just be careful about the version of Django you are installing.

假设你从上述链接中获取了你的归档文件,它应该是类似 Django-x.xx.tar.gz 的内容:

Let’s say you got your archive from the link above, it should be something like Django-x.xx.tar.gz:

提取并安装。

Extract and install.

$ tar xzvf Django-x.xx.tar.gz
$ cd Django-x.xx
$ sudo python setup.py install

你可以通过运行此命令来测试你的安装——

You can test your installation by running this command −

$ django-admin.py --version

如果在屏幕上看到打印的当前 Django 版本,则表明一切就绪。

If you see the current version of Django printed on the screen, then everything is set.

Note - 对于某些版本的 Django,它将是 django-admin,其中的“.py”被删除。

Note − For some version of Django it will be django-admin the ".py" is removed.

Windows Installation

我们假设你的电脑上已经安装了 Django 归档文件和 python。

We assume you have your Django archive and python installed on your computer.

首先,PATH 验证。

First, PATH verification.

在某些版本的 Windows(Windows 7)上,你可能需要确保 Path 系统变量包含以下路径:C:\Python34\;C:\Python34\Lib\site-packages\django\bin\,当然,这取决于你的 Python 版本。

On some version of windows (windows 7) you might need to make sure the Path system variable has the path the following C:\Python34\;C:\Python34\Lib\site-packages\django\bin\ in it, of course depending on your Python version.

然后提取并安装 Django。

Then, extract and install Django.

c:\>cd c:\Django-x.xx

接下来,通过运行以下命令安装 Django,在 Windows Shell “cmd” 中需要对此命令拥有管理员权限 −

Next, install Django by running the following command for which you will need administrative privileges in windows shell "cmd" −

c:\Django-x.xx>python setup.py install

若要测试安装,请打开命令提示符并键入以下命令 −

To test your installation, open a command prompt and type the following command −

c:\>python -c "import django; print(django.get_version())"

如果在屏幕上看到当前版本的 Django 打印出来,则一切就绪。

If you see the current version of Django printed on screen, then everything is set.

OR

启动 “cmd” 提示符并依次键入 python −

Launch a "cmd" prompt and type python then −

c:\> python
>>> import django
>>> django.VERSION

Step 3 – Database Setup

Django 支持多个主要的数据库引擎,您可以根据自己的习惯来设置其中的任何一个。

Django supports several major database engines and you can set up any of them based on your comfort.

您可以参考相关文档来安装和配置您选择的数据库。

You can refer to respective documentation to installing and configuring a database of your choice.

Note − 5 号和 6 号是 NoSQL 数据库。

Note − Number 5 and 6 are NoSQL databases.

Step 4 – Web Server

Django 带有一个用于开发和测试应用程序的轻量级 Web 服务器。此服务器预配置为与 Django 配合使用,更重要的是,每当您修改代码时都会重新启动。

Django comes with a lightweight web server for developing and testing applications. This server is pre-configured to work with Django, and more importantly, it restarts whenever you modify the code.

然而,Django 的确支持 Apache 和其他流行的 Web 服务器,例如 Lighttpd。我们将在处理不同示例时在后续章节中讨论这两种方法。

However, Django does support Apache and other popular web servers such as Lighttpd. We will discuss both the approaches in coming chapters while working with different examples.