How to install Django-Install Python Django | Django 安装指南【官方版】

How to install Django

This document will get you up and running with Django.

Install Python——Linux and MacOS

Django is a Python web framework. See What Python version can I use with Django? for details.

Get the latest version of Python at Download Python | Python.org or with your operating system’s package manager.

Python on Windows

If you are just starting with Django and using Windows, you may find How to install Django on Windows useful.

Install Apache and mod_wsgi

If you just want to experiment with Django, skip ahead to the next section; Django includes a lightweight web server you can use for testing, so you won’t need to set up Apache until you’re ready to deploy Django in production.

If you want to use Django on a production site, use Apache with mod_wsgi. mod_wsgi operates in one of two modes: embedded mode or daemon mode. In embedded mode, mod_wsgi is similar to mod_perl – it embeds Python within Apache and loads Python code into memory when the server starts. Code stays in memory throughout the life of an Apache process, which leads to significant performance gains over other server arrangements. In daemon mode, mod_wsgi spawns an independent daemon process that handles requests. The daemon process can run as a different user than the web server, possibly leading to improved security. The daemon process can be restarted without restarting the entire Apache web server, possibly making refreshing your codebase more seamless. Consult the mod_wsgi documentation to determine which mode is right for your setup. Make sure you have Apache installed with the mod_wsgi module activated. Django will work with any version of Apache that supports mod_wsgi.

See How to use Django with mod_wsgi for information on how to configure mod_wsgi once you have it installed.

If you can’t use mod_wsgi for some reason, fear not: Django supports many other deployment options. One is uWSGI; it works very well with nginx. Additionally, Django follows the WSGI spec (PEP 3333), which allows it to run on a variety of server platforms.

Get your database running

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 PostgreSQLMariaDBMySQLOracle 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.

In addition to the officially supported databases, there are backends provided by 3rd parties that allow you to use other databases with Django.

In addition to a database backend, you’ll need to make sure your Python database bindings are installed.

  • If you’re using PostgreSQL, you’ll need the psycopg2 package. Refer to the PostgreSQL notes for further details.
  • If you’re using MySQL or MariaDB, you’ll need a DB API driver like mysqlclient. See notes for the MySQL backend for details.
  • If you’re using SQLite you might want to read the SQLite backend notes.
  • If you’re using Oracle, you’ll need a copy of cx_Oracle, but please read the notes for the Oracle backend for details regarding supported versions of both Oracle and cx_Oracle.
  • If you’re using an unofficial 3rd party backend, please consult the documentation provided for any additional requirements.

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 SELECTINSERTUPDATE 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.

If you’re using Django’s testing framework to test database queries, Django will need permission to create a test database.

Install the Django code

Installation instructions are slightly different depending on whether you’re installing a distribution-specific package, downloading the latest official release, or fetching the latest development version.

Installing an official release with pip

This is the recommended way to install Django.

  1. Install pip. The easiest is to use the standalone pip installer. If your distribution already has pip installed, you might need to update it if it’s outdated. If it’s outdated, you’ll know because installation won’t work.

  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. After you’ve created and activated a virtual environment, enter the command:

    / 
    $ 
    
    python -m pip install Django

Installing a distribution-specific package

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.

Installing the development version

Tracking Django development

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

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

  1. Make sure that you have Git installed and that you can run its commands from a shell. (Enter git help at a shell prompt to test this.)

  2. Check out Django’s main development branch like so:

    / 
    $ 
    git clone https://github.com/django/django.git

    This will create a directory django in your current directory.

  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:

    / 
    $ 
    
    python -m pip install -e django/

    This will make Django’s code importable, and will also make the django-admin utility command available. In other words, you’re all set!

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.

===================== 中文版指南===============

如何安装 Django 

本文档将帮助您启动并运行 Django。

安装 Python 

Django 是一个 Python 网络框架。请参阅我可以在 Django 中使用哪个 Python 版本?详情。

在https://www.python.org/downloads/或使用操作系统的包管理器获取最新版本的 Python 。

Windows 上的 Python

如果您刚开始使用 Django 并使用 Windows,您可能会发现如何在 Windows 上安装 Django很有用。

安装 Apache 和mod_wsgi

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

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

有关安装 mod_wsgi 后如何配置它的信息,请参阅如何将 Django 与 mod_wsgi 一起使用。

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

让你的数据库运行

如果您打算使用 Django 的数据库 API 功能,则需要确保数据库服务器正在运行。Django 支持许多不同的数据库服务器,并得到PostgreSQLMariaDBMySQLOracleSQLite的正式支持。

如果您正在开发一个小型项目或不打算在生产环境中部署的东西,SQLite 通常是最佳选择,因为它不需要运行单独的服务器。但是,SQLite 与其他数据库有很多不同之处,因此如果您正在做一些实质性的工作,建议使用您计划在生产中使用的相同数据库进行开发。

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

除了数据库后端之外,您还需要确保安装了 Python 数据库绑定。

  • 如果您使用的是 PostgreSQL,则需要psycopg2包。有关详细信息,请参阅PostgreSQL 说明
  • 如果您使用 MySQL 或 MariaDB,则需要一个DB API 驱动程序,例如mysqlclient. 有关详细信息,请参阅MySQL 后端的注释。
  • 如果您使用 SQLite,您可能需要阅读SQLite 后端说明
  • 如果您使用的是 Oracle,则需要一份cx_Oracle的副本,但请阅读Oracle 后端的说明,了解有关 Oracle 和cx_Oracle.
  • 如果您使用的是非官方的第 3 方后端,请查阅提供的文档以了解任何其他要求。

如果您打算使用 Django 的命令为您的模型自动创建数据库表(在首次安装 Django 并创建项目之后),您需要确保 Django 有权在您正在使用的数据库中创建和更改表;如果您计划手动创建表,您可以授予 Django 、和权限。创建具有这些权限的数据库用户后,您将在项目的设置文件中指定详细信息,请参阅详细信息。manage.py migrateSELECTINSERTUPDATEDELETEDATABASES

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

安装 Django 代码

根据您是安装特定于发行版的软件包、下载最新的官方版本还是获取最新的开发版本,安装说明会略有不同。

使用¶安装官方版本pip

这是安装 Django 的推荐方式。

  1. 安装点子。最简单的是使用独立的 pip 安装程序。如果您的发行版已经pip安装,如果它已经过时,您可能需要更新它。如果它已过时,您会知道,因为安装将无法正常工作。

  2. 看看venv。该工具提供了隔离的 Python 环境,这比在系统范围内安装包更实用。它还允许在没有管理员权限的情况下安装软件包。贡献教程介绍了如何创建虚拟环境。

  3. 创建并激活虚拟环境后,输入命令:

    / 
    $ 
    python -m pip install Django

安装特定于发行版的包

检查发行版的具体说明,看看你的平台/发行版是否提供官方的 Django 包/安装程序。分发提供的软件包通常允许自动安装依赖项和支持的升级路径;然而,这些包很少会包含最新版本的 Django。

安装开发版本

跟踪 Django 开发

如果您决定使用 Django 的最新开发版本,您需要密切关注开发时间线,并密切关注即将发布的版本说明。这将帮助您掌握可能想要使用的任何新功能,以及在更新 Django 副本时需要对代码进行的任何更改。(对于稳定版本,任何必要的更改都记录在发行说明中。)

如果您希望能够偶尔使用最新的错误修复和改进来更新您的 Django 代码,请按照以下说明进行操作:

  1. 确保您已安装Git,并且您可以从 shell 运行它的命令。(在 shell 提示符下输入以测试它。)git help

  2. 查看 Django 的主要开发分支,如下所示:

    / 
    $ 
    git clone https://github.com/django/django.git

    django这将在您的当前目录中创建一个目录。

  3. 确保 Python 解释器可以加载 Django 的代码。最方便的方法是使用虚拟环境和pip贡献教程介绍了如何创建虚拟环境。

  4. 设置并激活虚拟环境后,运行以下命令:

    / 
    $ 
    python -m pip install -e django/

    这将使 Django 的代码可导入,并且还将使django-admin实用程序命令可用。换句话说,你已经准备好了!

如果要更新 Django 源代码的副本,请从目录中运行命令。执行此操作时,Git 将下载所有更改。git pulldjango

=======================Windows 安装Django =================

How to install Django

This document will get you up and running with Django.

Install Python

Django is a Python web framework. See What Python version can I use with Django? for details.

Get the latest version of Python at Download Python | Python.org or with your operating system’s package manager.

Python on Windows

If you are just starting with Django and using Windows, you may find How to install Django on Windows useful.

Install Apache and mod_wsgi

If you just want to experiment with Django, skip ahead to the next section; Django includes a lightweight web server you can use for testing, so you won’t need to set up Apache until you’re ready to deploy Django in production.

If you want to use Django on a production site, use Apache with mod_wsgi. mod_wsgi operates in one of two modes: embedded mode or daemon mode. In embedded mode, mod_wsgi is similar to mod_perl – it embeds Python within Apache and loads Python code into memory when the server starts. Code stays in memory throughout the life of an Apache process, which leads to significant performance gains over other server arrangements. In daemon mode, mod_wsgi spawns an independent daemon process that handles requests. The daemon process can run as a different user than the web server, possibly leading to improved security. The daemon process can be restarted without restarting the entire Apache web server, possibly making refreshing your codebase more seamless. Consult the mod_wsgi documentation to determine which mode is right for your setup. Make sure you have Apache installed with the mod_wsgi module activated. Django will work with any version of Apache that supports mod_wsgi.

See How to use Django with mod_wsgi for information on how to configure mod_wsgi once you have it installed.

If you can’t use mod_wsgi for some reason, fear not: Django supports many other deployment options. One is uWSGI; it works very well with nginx. Additionally, Django follows the WSGI spec (PEP 3333), which allows it to run on a variety of server platforms.

Get your database running

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 PostgreSQLMariaDBMySQLOracle 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.

In addition to the officially supported databases, there are backends provided by 3rd parties that allow you to use other databases with Django.

In addition to a database backend, you’ll need to make sure your Python database bindings are installed.

  • If you’re using PostgreSQL, you’ll need the psycopg2 package. Refer to the PostgreSQL notes for further details.
  • If you’re using MySQL or MariaDB, you’ll need a DB API driver like mysqlclient. See notes for the MySQL backend for details.
  • If you’re using SQLite you might want to read the SQLite backend notes.
  • If you’re using Oracle, you’ll need a copy of cx_Oracle, but please read the notes for the Oracle backend for details regarding supported versions of both Oracle and cx_Oracle.
  • If you’re using an unofficial 3rd party backend, please consult the documentation provided for any additional requirements.

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 SELECTINSERTUPDATE 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.

If you’re using Django’s testing framework to test database queries, Django will need permission to create a test database.

Install the Django code

Installation instructions are slightly different depending on whether you’re installing a distribution-specific package, downloading the latest official release, or fetching the latest development version.

Installing an official release with pip

This is the recommended way to install Django.

  1. Install pip. The easiest is to use the standalone pip installer. If your distribution already has pip installed, you might need to update it if it’s outdated. If it’s outdated, you’ll know because installation won’t work.

  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. After you’ve created and activated a virtual environment, enter the command:

    / 
    ...\> py -m pip install Django
    

Installing a distribution-specific package

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.

Installing the development version

Tracking Django development

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

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

  1. Make sure that you have Git installed and that you can run its commands from a shell. (Enter git help at a shell prompt to test this.)

  2. Check out Django’s main development branch like so:

    / 
    ...\> git clone https://github.com/django/django.git
    

    This will create a directory django in your current directory.

  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:

    / 
    ...\> py -m pip install -e django\
    

    This will make Django’s code importable, and will also make the django-admin utility command available. In other words, you’re all set!

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.

posted @ 2022-04-28 11:55  太阳神LoveU  阅读(1)  评论(0编辑  收藏  举报  来源