本文共 7057 字,大约阅读时间需要 23 分钟。
Let’s look at how to bootstrap a Django Project pre-loaded with the basic requirements needed in order to quickly get a project up and running. Further, beyond the project structure, most bootstrapped projects also take care of setting up the development and production environment settings, without troubling the user much – so we’ll look at that as well.
让我们看一下如何引导一个预加载了基本要求的Django Project,以便快速启动并运行一个项目。 此外,除了项目结构之外,大多数自举项目还负责设置开发和生产环境设置,而不会给用户带来太多麻烦,因此我们也会对此进行研究。
We’ll be using the popular as the bootstrapper for our Django Project along with to manage our application environment.
我们将使用流行的作为Django项目的引导程序,并使用来管理我们的应用程序环境。
Let’s begin!
让我们开始!
Start by installing globally:
首先全局安装 :
11 |
Now execute the following command to generate a bootstrapped django project:
现在执行以下命令来生成一个引导的django项目:
11 |
This command runs cookiecutter with the repo, allowing us to enter project-specific details:
此命令使用库运行cookiecutter,使我们能够输入特定于项目的详细信息:
1122334455667788991010111112121313141415151616171718181919202021212222232324242525 |
Take a quick look at the generated project structure, taking specific note of the following directories:
快速浏览一下生成的项目结构,特别注意以下目录:
pip install -r file_name
.users
app containing the models and boilerplate code associated with user authentication.pip install -r file_name
进行pip install -r file_name
。 users
应用程序组成。 Start by downloading and then installing the (v) to obtain virtualbox and the required Docker components:
首先下载并安装 (v ),以获得virtualbox和所需的Docker组件:
Once installed, create a new Docker host within the root of the newly created Django Project:
安装后,在新创建的Django项目的根目录中创建一个新的Docker主机:
1122 |
NOTE:
dev
can be named anything you want. For example, if you have more than one development environment, you could name themdjangodev1
,djangodev2
, and so forth.注意 :
dev
可以命名为任何您想要的名称。 例如,如果您有多个开发环境,则可以将它们命名为djangodev1
,djangodev2
等。
To view all Machines, run:
要查看所有计算机,请运行:
11 |
You can also view the IP of the dev
Machine by running:
您还可以通过运行以下命令查看dev
机器的IP:
11 |
Finally, let’s create a “/data” partition within the VM itself so that changes are persistent:
最后,让我们在VM本身中创建一个“ / data”分区,以使更改持久化:
112233 |
Now we can fire everything up – e.g., Django and Postgres – via Docker Compose:
现在,我们可以通过Docker Compose启动所有内容,例如Django和Postgres:
1122 |
Running Windows? Hit this error –
Interactive mode is not yet supported on Windows
? See .运行Windows? 遇到此错误–
Interactive mode is not yet supported on Windows
? 看到 。
The first build will take a while. Due to , subsequent builds will run much faster.
第一次构建将花费一些时间。 由于 ,后续构建将运行得更快。
Now we can test our Django Project by applying the migrations and then running the server:
现在,我们可以通过应用迁移并运行服务器来测试Django项目:
112233 |
Navigate to the dev
IP (port 8000) in your browser to view the Project quick start page with debugging mode on and many more development environment oriented features installed and running.
导航到浏览器中的dev
IP(端口8000),以打开调试模式并安装并运行许多面向开发环境的功能,查看Project快速入门页。
Kill the server, initialize a new git repo, commit, and PUSH to GitHub.
终止服务器,初始化一个新的git repo,提交并推送到GitHub。
So, we have successfully set up our Django Project locally using cookiecutter-django and served it up using the traditional manage.py command line utility via Docker.
因此,我们已经成功地使用cookiecutter-django在本地设置了Django项目,并通过Docker使用了传统的manage.py命令行实用程序来为其提供服务。
In this section, we move on to the deployment part, where the role of a web server comes into play. We will be setting up a Docker Machine on a droplet, with Postgres as our database and as our web server.
在本节中,我们继续进行部署部分,其中将发挥Web服务器的作用。 我们将在 Droplet上设置Docker Machine,以Postgres作为我们的数据库,以作为我们的Web服务器。
Along with this, we will be making use of instead of Django’s single-threaded development server to run the server process.
同时,我们将使用而不是Django的单线程开发服务器来运行服务器进程。
Apart from being a high-performance HTTP server, which almost every good web server out in the market is, Nginx has some really good features that make it stand out from the rest – namely that it:
Nginx除了是高性能HTTP服务器(几乎市场上所有优秀的Web服务器都是这种服务器)之外,还具有一些非常好的功能,使其与众不同,即:
is a Python WSGI HTTP server that can be easily customized and provides better performance in terms of reliability than Django’s single-threaded development server within production environments.
是Python WSGI HTTP服务器,可以在生产环境中轻松定制,并且在可靠性方面提供比Django单线程开发服务器更好的性能。
We will be using a server for this tutorial. After you (if necessary), a Personal Access Token, and then run the following command:
在本教程中,我们将使用服务器。 (如有必要), 个人访问令牌,然后运行以下命令:
11223344 |
This should only take few minutes to provision the Digital Ocean droplet and set up a new Docker Machine called prod
. While you wait, navigate to the ; you should see a new droplet being created, again, called prod
.
只需花费几分钟即可配置Digital Ocean Droplet,并设置一个名为prod
的新Docker Machine。 等待期间,导航至“ ;然后单击“确定”。 您应该看到又创建了一个新的液滴,称为prod
。
Once done, there should now be two machines running, one locally (dev
) and one on Digital Ocean (prod
). Run docker-machine ls
to confirm:
完成后,现在应该有两台计算机在运行,一台在本地( dev
),一台在Digital Ocean( prod
)。 运行docker-machine ls
确认:
112233 |
Set prod
as the active machine and then load the Docker environment into the shell:
将prod
设置为活动机器,然后将Docker环境加载到shell中:
11 |
Start by renaming env.example to .env. Update the DJANGO_ALLOWED_HOSTS
variable to match the Digital Ocean IP address – i.e., DJANGO_ALLOWED_HOSTS=159.203.77.132
. Keep the remaining defaults for now.
首先将env.example重命名为.env。 更新DJANGO_ALLOWED_HOSTS
变量以匹配数字海洋IP地址,即DJANGO_ALLOWED_HOSTS=159.203.77.132
。 现在保留其余默认值。
Now we can create the build and then fire up the services in the cloud:
现在,我们可以创建构建,然后在云中启动服务:
1122 |
Apply all the migrations:
应用所有迁移:
1122 |
That’s it!
而已!
Now just visit your server’s IP address, associated with the Digital Ocean droplet, and view it in the browser.
现在,只需访问与Digital Ocean Drop关联的服务器IP地址,然后在浏览器中查看它即可。
You should be good to go.
你应该很好。
翻译自:
转载地址:http://swhwd.baihongyu.com/