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

news/2024/7/4 23:00:49

介绍 (Introduction)

Django is a free and open-source web framework written in Python with its core principles being 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.

Django是一个用Python编写的免费开源Web框架,其核心原则是可伸缩性,可重用性和快速开发。 还以其框架级别的一致性和松散耦合而闻名,它允许各个组件彼此独立。

In this tutorial, we will set up a Django environment for development purposes on an Ubuntu 20.04 server. For a live website, you will have additional considerations, including connecting to a database, setting up a domain name, and adding layers of security. We have a variety of tutorials on Django that can help support you as you build under our Django tag.

在本教程中,我们将在Ubuntu 20.04服务器上为开发目的设置Django环境。 对于实时网站,您将具有其他注意事项,包括连接到数据库,设置域名和增加安全性。 我们在Django上有许多教程,可以帮助您在使用Django标签进行构建时为您提供支持。

先决条件 (Prerequisites)

In order to complete this tutorial, you will need:

为了完成本教程,您将需要:

  • A non-root user account with sudo privileges, which you can achieve by following and completing the initial server setup for Ubuntu 20.04 tutorial.

    具有sudo特权的非root用户帐户,您可以通过遵循并完成Ubuntu 20.04教程的初始服务器设置来实现。

  • Python 3 set up with a virtual programming environment. You can get this set up via our Python 3 installation guide.

    Python 3设置了虚拟编程环境。 您可以通过我们的Python 3安装指南进行设置 。

第1步-安装Django (Step 1 — Installing Django)

There are several ways to install Django, the Python package manager pip within a virtual environment.

有几种方法可以在虚拟环境中安装Django(Python软件包管理器pip)。

While in the server’s home directory, we’ll 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. We’ll call it the generic env, but you should use a name that is meaningful for you and your project.

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.

在环境中,使用pip安装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
3.0.6

With Django installed on your server, we can move on to creating a test project to make sure everything is working correctly. We’ll be creating a skeleton web application.

在服务器上安装了Django之后,我们可以继续创建测试项目,以确保一切正常。 我们将创建一个框架式Web应用程序。

步骤2 —调整防火墙设置 (Step 2 — Adjusting Firewall Settings)

If you followed our initial server setup tutorial or have a firewall running on your server, we’ll need to open the port we’ll be using in our server’s firewall. For the UFW firewall, 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 modifying the inbound rules.

如果您使用的是DigitalOcean防火墙,则可以从入站规则中选择HTTP 。 您可以阅读有关DigitalOcean防火墙以及通过修改入站规则为其创建规则的更多信息。

步骤3 —启动项目 (Step 3 — 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 asgi.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项目的入口点。

  • asgi.py contains the configuration for the optional deployment to the Asynchronous Server Gateway Interface or ASGI, which provides a standard for apps that are either synchronous and asynchronous, and is considered to be a successor of WSGI (see below).

    asgi.py包含用于到异步服务器网关接口或ASGI的可选部署的配置,该配置为同步和异步的应用程序提供了标准,并被认为是WSGI的后继产品(请参见下文)。

  • 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 or WSGI, which provides a standard for synchronous Python apps.

    wsgi.py包含Web服务器网关接口或WSGI的配置,该配置为同步Python应用程序提供了标准。

Note: Although default files are generated, you still have the ability to tweak the asgi.py or wsgi.py files at any time to fit your deployment needs.

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

第4步-配置Django (Step 4 — Configuring Django)

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, 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. Next, we’ll go on to access our web app via a browser.

您可以通过按住CTRL + x键然后按y键来保存更改并退出nano。 接下来,我们将继续通过浏览器访问我们的Web应用程序。

Finally, let’s create an administrative user so that you can use the Djano admin interface. Let’s do this with the createsuperuser command:

最后,让我们创建一个管理用户,以便您可以使用Djano admin界面 。 让我们使用createsuperuser命令执行此createsuperuser

  • python manage.py createsuperuser

    python manage.py createsuperuser

You will be prompted for a username, an email address, and a password for your user.

系统将提示您输入用户名,电子邮件地址和用户密码。

第5步-访问Django Web App (Step 5 — Accessing the Django Web App)

With our configuration 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 0.0.0.0:8000

    python manage.py runserver 0.0.0.0: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 see the following:

页面加载后,您将看到以下内容:

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

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

To access the admin interface, add /admin/ to the end of your URL:

要访问管理界面,请在网址末尾添加/admin/

http://your_server_ip:8000/admin/

This will take you to a login screen:

这将带您进入登录屏幕:

If you enter the admin username and password that you just created, you will have access to the main admin section of the site:

如果输入刚刚创建的管理员用户名和密码,则可以访问该站点的主要管理员部分:

For more information about working with the Django admin interface, please see “How To Enable and Connect the Django Admin Interface.”

有关使用Django管理界面的更多信息,请参阅“如何启用和连接Django管理界面”。

When you are done with testing your app, you can press CTRL + C to stop the runserver command. This will return you to 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 installed Django and set up a development environment to begin working on your Django app.

在本教程中,您已经成功安装了Django,并设置了开发环境以开始在Django应用程序上工作。

You now have the foundation 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-20-04


http://www.niftyadmin.cn/n/3648281.html

相关文章

[职场生存]细节和感觉[一]:细节

[职场生存]细节和感觉[一]&#xff1a;细节 zhengyun_ustc 200701全文链接&#xff1a; [职场生存]细节和感觉[一][职场生存]细节和感觉[二]&#xff1a;细节包括哪些部分&#xff1f;[职场生存]细节和感觉[三]&#xff1a;感觉刚刚进入软件行业的时候&#xff0c;…

如何在Ubuntu 20.04上安装Anaconda Python发行版

介绍 (Introduction) Anaconda is an open-source package manager, environment manager, and distribution of the Python and R programming languages. It is commonly used for data science, machine learning, large-scale data processing, scientific computing, and …

如何在Ubuntu 20.04上安装和保护Redis [快速入门]

介绍 (Introduction) Redis is an in-memory key-value store known for its flexibility, performance, and wide language support. This quickstart tutorial demonstrates how to install, configure, and secure Redis on an Ubuntu 20.04 server. Redis是一个内存键值存储…

[职场生存]细节和感觉[二]:细节包括哪些部分?

[职场生存]细节和感觉[二]&#xff1a;细节包括哪些部分&#xff1f; zhengyun_ustc 200701前文链接&#xff1a;[职场生存]细节和感觉[一]前面说了细节是一种技巧&#xff0c;并且展示了老外的细节之一。那么到底我所说的细节都包括哪些部分呢&#xff1f;简单地说&#xf…

Java单元测试之JUnit篇

单元测试是编写测试代码&#xff0c;应该准确、快速地保证程序基本模块的正确性。 好的单元测试的标准 JUnit是Java单元测试框架&#xff0c;已经在Eclipse中默认安装。 JUnit4 JUnit4通过注解的方式来识别测试方法。目前支持的主要注解有&#xff1a; BeforeClass 全局只会…

手语翻译系统_如何建立一个神经网络将手语翻译成英语

手语翻译系统The author selected Code Org to receive a donation as part of the Write for DOnations program. 作者选择Code Org接受捐赠&#xff0c;这是Write for DOnations计划的一部分。 介绍 (Introduction) Computer vision is a subfield of computer science that…

[职场生存]细节和感觉[三]:感觉

[职场生存]细节和感觉[三]&#xff1a;感觉 zhengyun_ustc 200701前文链接&#xff1a;[职场生存]细节和感觉[一][职场生存]细节和感觉[二]&#xff1a;细节包括哪些部分&#xff1f;下面我们来说第二点&#xff1a;感觉。我所说的这个感觉不是以前我曾经强调过的“在工作中…

配置管理 ansible_Ansible配置管理简介

配置管理 ansible介绍 (Introduction) Configuration management is the process of handling changes to a system in a way that assures integrity over time, typically involving tools and processes that facilitate automation and observability. Even though this co…