如何安装 Django

本文档将帮助您使用 Django。

安装 Python

Django 是一个 Python Web 框架。参见 我应该使用哪个版本的 Python 来配合 Django? 获取更多细节。

可以通过 https://www.python.org/downloads/ 或者操作系统的包管理工具获取最新版本的 Python。

Windows 上的 Python

如果您刚刚开始学习 Django 并且使用 Windows,查看 如何在 Windows 上安装 Django 可能对你有帮助。

安装 Apache 和 mod_wsgi

如果您只是想试验 Django,请跳到下一部分;Django 包含一个可用于测试的轻量级 Web 服务器,因此在准备好在生产环境中部署 Django 之前,您不需要设置 Apache。

如果要在生产站点上使用 Django,请将 Apachemod_wsgi 一起使用。 mod_wsgi 能以两种模式运行:嵌入模式或守护进程模式。在嵌入模式下,mod_wsgi 类似于 mod_perl —— 它在 Apache 中嵌入 Python,并在服务器启动时将 Python 代码加载到内存中。代码在 Apache 进程的整个生命周期中都保留在内存中,与其他服务器相比,这可以显着提高性能。在守护进程模式下,mod_wsgi 会生成一个处理请求的独立守护进程。守护进程可以作为与 Web 服务器不同的用户运行,可能会提高安全性。可以在不重新启动整个 Apache Web 服务器的情况下重新启动守护进程,从而可以更加无缝地刷新代码库。请参阅 mod_wsgi 文档以确定适合您的设置的模式。确保你在安装了 Apache 并且启用了 mod_wsgi 模块。 Django 将在任何支持 mod_wsgi 的 Apache 版本上工作。

若已安装 mod_wsgi 模块,请查看 Django 如何利用 mod_wsgi 工作 了解如何配置。

如果由于某种原因你不能使用 mod_wsgi,请不要担心: Django 支持许多其他部署选项。一个是 uWSGI ;它和 nginx 配合使用很好。此外,Django 遵循 WSGI 规范( PEP 3333 ),允许它在各种服务器平台上运行。

运行你的数据库

If you plan to use Django’s database API functionality, you’ll need to make sure a database server is running. Django supports many different database servers and is officially supported with PostgreSQL, MariaDB, MySQL, Oracle and SQLite.

If you are developing a small project or something you don’t plan to deploy in a production environment, SQLite is generally the best option as it doesn’t require running a separate server. However, SQLite has many differences from other databases, so if you are working on something substantial, it’s recommended to develop with the same database that you plan on using in production.

除了官方支持的数据库,还有 第三方提供的后端 允许你在 Django 中使用其他数据库。

除了数据库后端,你还要确保安装了 Python 数据库绑定。

If you plan to use Django’s manage.py migrate command to automatically create database tables for your models (after first installing Django and creating a project), you’ll need to ensure that Django has permission to create and alter tables in the database you’re using; if you plan to manually create the tables, you can grant Django SELECT, INSERT, UPDATE and DELETE permissions. After creating a database user with these permissions, you’ll specify the details in your project’s settings file, see DATABASES for details.

如果你正在使用 Django 的 测试框架 来测试数据库查询,Django 将需要创建测试数据库的权限。

安装 Django 源码

安装过程可能会有些许差异,这取决于你是否在安装一个发行版——某个特定的版本,下载最新的正式发布包,或获取最新的开发版本。

通过 pip 安装正式发布版本

以下是安装 Django 的推荐方式。

  1. 安装 pip。最简单的方式是使用 独立 pip 安装器。若你的系统早已安装 pip,你可能需要更新它,因为它可能过期了。如果它过期了,你会知道的,因为过期的用不了。

  2. Take a look at venv. This tool provides isolated Python environments, which are more practical than installing packages systemwide. It also allows installing packages without administrator privileges. The contributing tutorial walks through how to create a virtual environment.

  3. 在你已创建并激活一个虚拟环境后,输入以下命令:

    Linux/Unix   Windows

    1. $ python -m pip install Django
    1. ...\> py -m pip install Django

安装特定发行版

Check the distribution specific notes to see if your platform/distribution provides official Django packages/installers. Distribution-provided packages will typically allow for automatic installation of dependencies and supported upgrade paths; however, these packages will rarely contain the latest release of Django.

安装开发版本

跟踪 Django 开发

如果你决定使用 Django 的最新开发版,你需要关注 开发版时间轴即将到来的新特性的发布说明。这将保证你能获取所有新特性和最新的代码。(对于稳定发布版,所有必要的修改都在发布说明中记录。)

如果你希望偶尔能获取最新的补丁和改进,遵循以下说明:

  1. 确保你已安装了 Git,这样你就可以从 shell 运行对应命令。(在 shell 中输入 git help 测试是否安装。)

  2. 像这样检出 Django 的主开发分支:

    Linux/Unix   Windows

    1. $ git clone https://github.com/django/django.git
    1. ...\> git clone https://github.com/django/django.git

    这会在当前目录创建一个 django 目录。

  3. Make sure that the Python interpreter can load Django’s code. The most convenient way to do this is to use a virtual environment and pip. The contributing tutorial walks through how to create a virtual environment.

  4. After setting up and activating the virtual environment, run the following command:

    Linux/Unix   Windows

    1. $ python -m pip install -e django/
    1. ...\> py -m pip install -e django\

    这会让 Django 的代码可导入,使得 django-admin 命令行工具可用。换句话说,大事可为。

When you want to update your copy of the Django source code, run the command git pull from within the django directory. When you do this, Git will download any changes.