如何安装Django

    Django is a Python Web framework. See 我应该使用哪个版本的 Python 来配合 Django? fordetails.

    获取最新版本的Python可以通过:访问 ;或者操作系统的包管理工具。

    基于 Jython 的 Django

    Jython (一个针对Java平台的Python实现)和Python 3不兼容,所以Django 2.0及以上的版本无法在Jython上运行。

    Windows上的Python

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

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

    如果要在生产站点上使用Django,请将 Apache 与 一起使用。 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模块,请查看 How to use Django with mod_wsgi

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

    如果你打算使用Django的数据库API功能,则需要确保数据库服务器正在运行。Django支持很多不同的数据库服务器,并且正式支持 PostgreSQL, , Oracle 和 。

    除了官方支持的数据库,还有 backends provided by 3rd parties 允许你在Django中使用其他数据库。

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

    • 如果你正在使用PostgreSQL,你需要 包。相关详细信息请参阅 PostgreSQL notes
    • 如果你正在使用MySQL,则需要一个像 mysqlclient``这样的 DB API driver 。相关详细信息请参阅 。
    • 如果你正在使用SQLite,则可能需要阅读 SQLite backend notes
    • 如果你正在使用Oracle,则需要 的副本,但请阅读 notes for the Oracle backend 以获取有关 Oracle 和 的支持的版本的详细信息。
    • 如果你使用的是非官方的第三方后端,请参阅提供的文档以了解任何其他要求。
      如果您打算使用Django的 manage.py migrate 命令为您的模型自动创建数据库表(在首次安装Django并创建项目之后),您需要确保Django有权在您正在使用的数据库中创建和更改表;如果你打算手动创建这些表,你可以只需授予Django SELECTINSERT, 和 DELETE``权限。创建具有这些权限的数据库用户后,您要在项目的配置文件中指定详细信息,请参阅 DATABASES 以获取详细信息。

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

    Installation instructions are slightly different depending on whether you'reinstalling a distribution-specific package, downloading the latest officialrelease, or fetching the latest development version.

    It's easy, no matter which way you choose.

    This is the recommended way to install Django.

    • Install pip. The easiest is to use the . If yourdistribution already has pip installed, you might need to update it ifit's outdated. If it's outdated, you'll know because installation won'twork.

    • After you've created and activated a virtual environment, enter the command:

    /

    1. ...\> pip install Django

    Check the distribution specific notes to see ifyour platform/distribution provides official Django packages/installers.Distribution-provided packages will typically allow for automatic installationof dependencies and easy upgrade paths; however, these packages will rarelycontain the latest release of Django.

    If you decide to use the latest development version of Django,you'll want to pay close attention to ,and you'll want to keep an eye on the release notes for theupcoming release. This will help you stayon top of any new features you might want to use, as well as any changesyou'll need to make to your code when updating your copy of Django.(For stable releases, any necessary changes are documented in therelease notes.)

    If you'd like to be able to update your Django code occasionally with thelatest bug fixes and improvements, follow these instructions:

    • Make sure that you have installed and that you can run its commandsfrom a shell. (Enter git help at a shell prompt to test this.)

    • Check out Django's main development branch like so:

    /

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

    This will create a directory in your current directory.

    • Make sure that the Python interpreter can load Django's code. The mostconvenient way to do this is to use virtualenv, , andpip. The walks throughhow to create a virtualenv.

    /

    1. ...\> pip install -e django\

    This will make Django's code importable, and will also make thedjango-admin utility command available. In other words, you're allset!

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