如何在Ubuntu 16.04上安装Django并设置开发环境

介绍 (Introduction)

Django is a free and open-source web framework written in Python that adheres to the model template view (MTV) software architectural pattern. The MTV pattern is Django’s take on the model–view–controller (MVC) pattern. According to the Django Software Foundation, the model is the single definitive source of your data, the view describes the data that gets represented to the user via a Python callback function to a specific URL, and the template is how Django generates HTML dynamically.

Django是一个使用Python编写的免费开放源代码Web框架,它遵循模型模板视图(MTV)软件体系结构模式。 MTV模式是Django在模型-视图-控制器(MVC)模式下的采用。 根据Django软件基金会的说法,该模型是数据的唯一确定来源,该视图描述了通过Python回调函数向特定URL表示给用户的数据,而模板是Django动态生成HTML的模板

Django’s core principles are scalability, re-usability and rapid development. It is also known for its framework-level consistency and loose coupling, allowing for individual components to be independent of one another. Don’t repeat yourself (DRY programming) is an integral part of Django principles.

Django的核心原则是可伸缩性,可重用性和快速开发。 还以其框架级别的一致性和松散耦合而闻名,它允许各个组件彼此独立。 不要重复自己( DRY编程 )是Django原理的组成部分。

In this tutorial, we will set up a Django development environment. We’ll install Python 3, pip 3, Django and virtualenv in order to provide you with the tools necessary for developing web applications with Django.

在本教程中,我们将建立一个Django开发环境。 我们将安装Python 3,pip 3,Django和virtualenv ,以便为您提供使用Django开发Web应用程序所需的工具。

先决条件 (Prerequisites)

A non-root user account with sudo privileges set up on a Debian or Ubuntu Linux server. You can achieve these prerequisites by following and completing the initial server setup for Debian 8, or steps 1-4 in the initial server setup for Ubuntu 16.04 tutorial.

在Debian或Ubuntu Linux服务器上设置的具有sudo特权的非root用户帐户。 您可以通过遵循并完成Debian 8初始服务器设置Ubuntu 16.04教程的初始服务器设置中的步骤1-4来实现这些先决条件。

第1步-安装Python和pip (Step 1 — Install Python and pip)

To install Python we must first update the local APT repository. In your terminal window, we’ll input the command that follows. Note that the -y flag answers “yes” to prompts during the upgrade process. Remove the flag if you’d like the upgrade to stop for each prompt.

要安装Python,我们必须首先更新本地APT存储库。 在您的终端窗口中,我们将输入以下命令。 请注意, -y标志在升级过程中对提示回答“是”。 如果您希望每个提示都停止升级,请删除该标志。

  • sudo apt-get update && sudo apt-get -y upgrade

    sudo apt-get更新&& sudo apt-get -y升级

When prompted to configure grub-pc, you can press ENTER to accept the default, or configure as desired.

当提示您配置grub-pc ,您可以按ENTER接受默认设置,或根据需要进行配置。

It is recommended by the Django Software Foundation to use Python 3, so once everything is updated, we can install Python 3 by using the following command:

Django Software Foundation建议使用Python 3,因此,所有内容更新后,我们可以使用以下命令安装Python 3:

  • sudo apt-get install python3

    须藤apt-get install python3

To verify the successful installation of Python 3, run a version check with the python3 command:

要验证Python 3的安装是否成功,请使用python3命令运行版本检查:

  • python3 -V

    python3 -V

The resulting output will look similar to this:

结果输出将类似于以下内容:


   
   
Output
python 3.5.2

Now that we have Python 3 installed, we will also need pip in order to install packages from PyPi, Python’s package repository.

现在我们已经安装了Python 3,我们还需要pip以便从Python的软件包存储库PyPi安装软件包。

  • sudo apt-get install -y python3-pip

    须藤apt-get install -y python3-pip

To verify that pip was successfully installed, run the following command:

要验证是否成功安装了pip,请运行以下命令:

  • pip3 -V

    点3

You should see output similar to this:

您应该看到类似于以下的输出:


   
   
Output
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)

Now that we have pip installed, we have the ability to quickly install other necessary packages for a Python environment.

现在我们已经安装了pip,我们可以快速安装适用于Python环境的其他必要软件包。

第2步-安装virtualenv (Step 2 — Install virtualenv)

virtualenv is a virtual environment where you can install software and Python packages in a contained development space, which isolates the installed software and packages from the rest of your machine’s global environment. This convenient isolation prevents conflicting packages or software from interacting with each other.

virtualenv是一个虚拟环境 ,您可以在其中将软件和Python软件包安装在包含的开发空间中,从而将已安装的软件和软件包与计算机的其余全局环境隔离开。 这种方便的隔离可防止冲突的程序包或软件相互交互。

To install virtualenv, we will use the pip3 command, as shown below:

要安装virtualenv,我们将使用pip3命令,如下所示:

  • pip3 install virtualenv

    pip3安装virtualenv

Once it is installed, run a version check to verify that the installation has completed successfully:

安装完成后,运行版本检查以验证安装是否成功完成:

  • virtualenv --version

    virtualenv-版本

We should see the following output, or something similar:

我们应该看到以下输出或类似的输出:


   
   
Output
virtualenv 20.0.20 from /home/sammy/.local/lib/python3.5/site-packages/virtualenv/__init__.py

You have successfully installed virtualenv.

您已经成功安装了virtualenv

At this point, we can isolate our Django web application and its associated software dependencies from other Python packages or projects on our system.

此时,我们可以将Django Web应用程序及其关联的软件依赖项与系统上其他Python软件包或项目隔离。

第3步-安装Django (Step 3 — Install Django)

There are three ways to install Django. We will be using the pip method of installation for this tutorial, but let’s address all of the available options for your reference.

有三种安装Django的方法。 在本教程中,我们将使用pip安装方法,但让我们介绍所有可用的选项供您参考。

  • Option 1: Install Django within a virtualenv. This is ideal for when you need your version of Django to be isolated from the global environment of your server.

    选项1:在virtualenv安装Django。 当您需要将Django版本与服务器的全局环境隔离时,这是理想的选择。

  • Option 2: Install Django from Source. If you want the latest software or want something newer than what your Ubuntu APT repository offers, you can install directly from source. Note that opting for this installation method requires constant attention and maintenance if you want your version of the software to be up to date.

    选项2:从源代码安装Django。 如果您需要最新的软件或想要比Ubuntu APT存储库所提供的软件更新的产品,则可以直接从源代码进行安装。 请注意,如果您希望软件的版本是最新的,则选择这种安装方法需要不断的关注和维护。

  • Option 3: Install Django Globally with pip. The option we are going with is pip 3 as we will be installing Django globally.

    选项3:使用pip全局安装Django。 我们要使用的选项是pip 3,因为我们将在全球范围内安装Django。

We’ll be installing Django using pip within a virtual environment. For further guidance and information on the setup and utilization of programming environments, check out this tutorial on setting up a virtual environment.

我们将在虚拟环境中使用pip安装Django。 有关设置和使用编程环境的更多指导和信息,请参阅有关设置虚拟环境的本教程。

While in the server’s home directory, we have to create the directory that will contain our Django application. Run the following command to create a directory called django-apps, or another name of your choice. Then navigate to the directory.

在服务器的主目录中时,我们必须创建将包含Django应用程序的目录。 运行以下命令以创建名为django-apps的目录,或您选择的其他名称。 然后导航到目录。

  • mkdir django-apps

    mkdir django-apps

  • cd django-apps

    cd django应用程式

While inside the django-apps directory, create your virtual environment. Let’s call it env.

django-apps目录中,创建您的虚拟环境。 我们称之为env

  • virtualenv env

    的virtualenv ENV

Now, activate the virtual environment with the following command:

现在,使用以下命令激活虚拟环境:

  • . env/bin/activate

    。 env / bin / activate

You’ll know it’s activated once the prefix is changed to (env), which will look similar to the following depending on what directory you are in:

您会知道,一旦将前缀更改为(env) ,它就会被激活,这取决于您所在的目录,其外观类似于以下内容:

Within the environment, install the Django package using pip. Installing Django allows us to create and run Django applications. To learn more about Django, read our tutorial series on Django Development.

在环境中,使用pip安装Django软件包。 安装Django可让我们创建和运行Django应用程序。 要了解有关Django的更多信息,请阅读我们有关Django开发的系列教程。

  • pip install django

    pip安装Django

Once installed, verify your Django installation by running a version check:

安装完成后,通过运行版本检查来验证Django的安装:

  • django-admin --version

    django-admin --version

This, or something similar, will be the resulting output:

这或类似的东西将是结果输出:


   
   
Output
2.2.12

With Django installed on your server, we can move on to creating a test project to make sure everything is working correctly.

在服务器上安装了Django之后,我们可以继续创建测试项目,以确保一切正常。

第4步-创建Django测试项目 (Step 4 — Creating a Django Test Project)

To test the Django installation, we will be creating a skeleton web application.

为了测试Django的安装,我们将创建一个框架Web应用程序。

设置防火墙规则 (Setting Firewall Rules)

First, if applicable, we’ll need to open the port we’ll be using in our server’s firewall. If you are using UFW (as detailed in the initial server setup guide), you can open the port with the following command:

首先,如果适用,我们需要打开将在服务器防火墙中使用的端口。 如果使用的是UFW(如初始服务器设置指南中所述 ),则可以使用以下命令打开端口:

  • sudo ufw allow 8000

    sudo ufw允许8000

If you’re using DigitalOcean Firewalls, you can select HTTP from the inbound rules. You can read more about DigitalOcean Firewalls and creating rules for them by reading the inbound rules section of the introductory tutorial.

如果您使用的是DigitalOcean防火墙,则可以从入站规则中选择HTTP 。 您可以阅读入门教程入站规则部分,以了解有关DigitalOcean防火墙以及为它们创建规则的更多信息。

开始项目 (Starting the Project)

We now can generate an application using django-admin, a command line utility for administration tasks in Python. Then we can use the startproject command to create the project directory structure for our test website.

现在,我们可以使用django-admin生成一个应用程序,它是用于Python中管理任务的命令行实用程序。 然后,我们可以使用startproject命令为我们的测试网站创建项目目录结构。

While in the django-apps directory, run the following command:

django-apps目录中时,运行以下命令:

  • django-admin startproject testsite

    django-admin startproject测试站点

Note: Running the django-admin startproject <projectname> command will name both project directory and project package the <projectname> and create the project in the directory in which the command was run. If the optional <destination> parameter is provided, Django will use the provided destination directory as the project directory, and create manage.py and the project package within it.

注意:运行django-admin startproject <projectname>命令将同时命名项目目录和项目包<projectname>并在运行命令的目录中创建项目。 如果提供了可选的<destination>参数,则Django将使用提供的目标目录作为项目目录,并在其中创建manage.py及其项目包。

Now we can look to see what project files were just created. Navigate to the testsite directory then list the contents of that directory to see what files were created:

现在,我们可以看看刚刚创建了哪些项目文件。 导航到testsite目录,然后列出该目录的内容以查看创建了哪些文件:

  • cd testsite

    cd测试站点
  • ls

    ls

   
   
Output
manage.py testsite

You will notice output that shows this directory contains a file named manage.py and a folder named testsite. The manage.py file is similar to django-admin and puts the project’s package on sys.path. This also sets the DJANGO_SETTINGS_MODULE environment variable to point to your project’s settings.py file.

您会注意到输出显示此目录包含一个名为manage.py的文件和一个名为testsite的文件夹。 manage.py文件类似于django-admin ,并将项目的程序包放在sys.path 。 这还将设置DJANGO_SETTINGS_MODULE环境变量以指向您项目的settings.py文件。

You can view the manage.py script in your terminal by running the less command like so:

您可以通过运行less命令在终端中查看manage.py脚本,如下所示:

  • less manage.py

    少管家

When you’re finished reading the script, press q, to quit viewing the file.

阅读完脚本后,按q退出查看文件。

Now navigate to the testsite directory to view the other files that were created:

现在,导航到testsite目录以查看创建的其他文件:

  • cd testsite/

    cd testsite /

Then run the following command to list the contents of the directory:

然后运行以下命令以列出目录的内容:

  • ls

    ls

You will see four files:

您将看到四个文件:


   
   
Output
__init__.py settings.py urls.py wsgi.py

Let’s go over what each of these files are:

让我们看一下这些文件分别是什么:

  • __init__.py acts as the entry point for your Python project.

    __init__.py充当Python项目的入口点。

  • settings.py describes the configuration of your Django installation and lets Django know which settings are available.

    settings.py描述了Django安装的配置,并让Django知道哪些设置可用。

  • urls.py contains a urlpatterns list, that routes and maps URLs to their views.

    urls.py包含一个urlpatterns列表,该列表将URL路由并映射到其views

  • wsgi.py contains the configuration for the Web Server Gateway Interface. The Web Server Gateway Interface (WSGI) is the Python platform standard for the deployment of web servers and applications.

    wsgi.py包含Web服务器网关接口的配置。 Web服务器网关接口( WSGI )是用于部署Web服务器和应用程序的Python平台标准。

Note: Although a default file was generated, you still have the ability to tweak the wsgi.py at any time to fit your deployment needs.

注意:尽管已生成默认文件,但是您仍然可以随时调整wsgi.py以适应您的部署需求。

启动并查看您的网站 (Start and View your Website)

Now we can start the server and view the website on a designated host and port by running the runserver command.

现在,我们可以通过运行runserver命令启动服务器并在指定的主机和端口上查看网站。

We’ll need to add your server ip address to the list of ALLOWED_HOSTS in the settings.py file located in ~/test_django_app/testsite/testsite/.

我们需要将您的服务器IP地址添加到~/test_django_app/testsite/testsite/中的settings.py文件中的ALLOWED_HOSTS列表中。

As stated in the Django docs, the ALLOWED_HOSTS variable contains “a list of strings representing the host/domain names that this Django site can serve. This is a security measure to prevent HTTP Host header attacks, which are possible even under many seemingly-safe web server configurations.”

Django文档所述, ALLOWED_HOSTS变量包含“表示此Django站点可以服务的主机/域名的字符串列表。 这是一种安全措施,可以防止HTTP Host标头攻击,即使在许多看似安全的Web服务器配置下也可能发生这种攻击。”

You can use your favorite text editor to add your ip address. For example, if you’re using nano, just simply run the following command:

您可以使用喜欢的文本编辑器添加IP地址。 例如,如果您使用nano ,只需运行以下命令:

  • nano ~/django-apps/testsite/testsite/settings.py

    纳米〜/ django-apps / testsite / testsite / settings.py

Once you run the command, you’ll want to navigate to the Allowed Hosts Section of the document and add your server’s IP address inside the square brackets within single or double quotes.

运行命令后,您将需要导航到文档的“允许的主机”部分,并将服务器的IP地址添加在单引号或双引号内的方括号内。

settings.py
settings.py
"""
Django settings for testsite project.

Generated by 'django-admin startproject' using Django 2.0.
...
"""
...
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# Edit the line below with your server IP address
ALLOWED_HOSTS = ['your-server-ip']
...

You can save the change and exit nano by holding down the CTRL + x keys and then pressing the y key.

您可以通过按住CTRL + x键然后按y键来保存更改并退出nano。

With this completed, be sure to navigate back to the directory where manage.py is located:

完成此操作后,请确保导航回到manage.py所在的目录:

  • cd ~/django-apps/testsite/

    cd〜/ django-apps / testsite /

Now, run the following command replacing the your-server-ip text with the IP of your server:

现在,运行以下命令,将your-server-ip文本替换为服务器的IP:

  • python manage.py runserver your-server-ip:8000

    python manage.py runserver your-server-ip :8000

Finally, you can navigate to the below link to see what your skeleton website looks like, again replacing the highlighted text with your server’s actual IP:

最后,您可以导航到以下链接以查看您的骨架网站的外观,再次用服务器的实际IP替换突出显示的文本:

http://your-server-ip:8000/

Once the page loads, you’ll receive a webpage that is similar to the following:

页面加载后,您将收到类似于以下内容的网页:

This confirms that Django was properly installed and our test project is working correctly.

这确认Django已正确安装并且我们的测试项目正常运行。

When you are done with testing your app, you can press CTRL + C to stop the runserver command. This will return you to the your programming environment.

测试完应用程序后,可以按CTRL + C停止runserver命令。 这将使您返回到编程环境。

When you are ready to leave your Python environment, you can run the deactivate command:

当您准备离开Python环境时,可以运行deactivate命令:

  • deactivate

    停用

Deactivating your programming environment will put you back to the terminal command prompt.

停用编程环境将使您返回到终端命令提示符。

结论 (Conclusion)

In this tutorial you have successfully upgraded to the latest version of Python 3 available to you via the Ubuntu APT repository. You’ve also installed pip 3, virtualenv, and django.

在本教程中,您已成功通过Ubuntu APT存储库成功升级到了最新版本的Python 3。 您还安装了pip 3, virtualenvdjango

You now have the tools needed to get started building Django web applications.

现在,您已具有开始构建Django Web应用程序所需的工具。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-django-and-set-up-a-development-environment-on-ubuntu-16-04

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值