个人技术分享

Let’s look at the project files created for us.
让我们看看创建的项目文件。

Open the project folder backend in a code editor.
使用代码编辑器打开backend项目。

I will be using the VSCode editor in this book.
我会在这本书中使用vscode编辑器。

manage.py

You will see a file manage.py which we should not tinker.
你会看到一个manage.py文件。

manage.py helps us do administrative things.
manage.py文件帮助我们管理项目。

For example.
比如。

python manage.py runserver

在这里插入图片描述

to start the local web server.
用于启动本地web服务。

We will later illustrate more of its administrative functions e.g. creating a new app.
我们稍后会使用它更多的管理命令,比如创建一个新的应用。

python manage.py startapp xxx

db.sqlite3

We also have the db.sqlite3 file that contains our database.
我们同样拥有一个db.sqlite3文件,这个文件包含了我们的数据库信息。

We touch more on this file in the Models chapter.
我们会在Models模型管理章节讲解关于这个文件的更多内容。

backend folder

You will find another folder of the same name backend.
你会发现另一个文件夹,名字也叫作backend。

To avoid confusion and distinguish between the two folders, we will keep the inner backend folder as it is and rename the outer folder to todobackend.
为了避免两个文件夹混淆,我们会保持内存的文件夹叫做backend,而把外部的文件夹重命名为todobackend。

注意:这里实际上不需要改动,Django在所有版本中都是会默认创建一个和项目名同名的内部文件夹,这个文件夹存放了项目最核心的信息,比如配置文件。

In it, you might have a __pycache__ folder that stores compiled bytecode when we generate our project.
在内部,你可能会发现一个__pycache__文件夹,这个文件夹存放的是我们项目编译之后的信息。

You can largely ignore this folder.
你可以最大程度的忽略这个文件夹。

Its purpose is to make your project start a little faster by caching the compiled code so that it can be readily executed.
它可能可以让你的项目启动得更快,因为它缓存了可以被编译器直接运行的编译好的代码。

__init__.py

__init__.py specifies what to run when Django launches for the first time.
__init__.py 指定了Django启动的时候,第一时间应该运行的代码。

Again, we can ignore this file.
我们同样也可以忽略这个文件。

asgi.py

asgi.py allows for an optional Asynchronous Server Gateway Interface to run.
asgi.py 允许我们使用异步网关接口来启动我们的web服务。

asgi.py which stands for Web Server Gateway Interface helps Django serve our web pages.
asgi.py基于web网关接口帮助我们在浏览器中展示我们的web页面。

Both files are used when deploying our app.
两个文件都是用于部署我们的项目程序的。

We will revisit them later when we deploy our app.
我们会在稍后部署项目的章节中重新回顾这两个文件。

urls.py

This is the file which tells Django which pages to render in response to a browser or URL request.
这个文件告诉Django,浏览器请求某个URL的时候,该如何做出响应。

For example, when someone enters a url http://localhost:8000/123, the request comes into urls.py and gets routed to a page based on the paths specified there.
比如,当我们访问 http://localhost:8000/123 的时候,请求会到达 urls.py,让后urls.py根据在内部配置的路由规则,去响应指定的页面。

We will later add paths to this file and better understand how it works.
我们之后会在这个文件中添加路由,以便更好的理解它是如何工作的。

settings.py

The settings.py file is an important file controlling our project’s.
settings.py 文件是帮我们我们控制项目的最重要的文件。

It contains serveral properties:
它包含了一系列属性:

  • BASE_DIR determines where on your machine the project is situated at.
  • BASE_DIR 定义了项目位于你的机器上的具体哪个位置。
  • SECRETE_KEY is used when you have data flowing in and out of your website. Do not ever share this with others.
  • SECRETE_KEY 用于你数据输入输出的加密出来,永远不要和其他人分享你的SECRETE_KEY。
  • DEBUG is our site can run in debug or not. In debug mode, we get detailed information on errors. For e.g. if I run http://localhost:8000/123 in the browser, I will see a 404 page not found with details on the error.
  • DEBUG 用于控制我们的站点是否开启调试功能。调试模式会展示详细的错误信息。比如当我访问http://localhost:8000/123的时候,我会发现一个404错误页面,并附有详细的错误提示。

在这里插入图片描述

  • If DEBUG = False, we will see a generic 404 page without error details. While developing our project, we set DEBUG = True to help us with debugging. When deploying our app to production, we should set DEBUG to False.
  • 如果设置 DEBUG = False,我们将会得到一个通用的404页面,不会有详细的错误提示。我们开发项目的时候设置 DEBUG = True 用来帮助我们进行调试。当部署的时候,则一定要设置 DEBUG = False。
  • INSTALLED_APPS allow us to bring in different pieces of code into our project. We will see this in action later.
  • INSTALLED_APPS 允许我们将代码拆分为不同的应用进行管理,我们将会在接下来的实战中使用到它。
  • MIDDLEWARE are built-in Django functions to help in our project.
  • MIDDLEWARE 是Django内置的功能,用来帮助我们强化项目的功能。
  • ROOT_URLCONF specify where our URLs are.
  • ROOT_URLCONF 指定我们的路由使用具体哪个urls.py文件。
  • TEMPLATES help us return HTML code.
  • TEMPLATES 帮助我们返回HTML代码。
  • AUTH_PASSWORD_VALIDATORS allow us to specify validations we want on passwords. E.g. a minimum length.
  • AUTH_PASSWORD_VALIDATORS 允许我们指定密码的校验方式,比如最小长度。

There are some other properties in settings.py like LANGUAGE_CODE and TIME_ZONE but we have focused on the more important ones.
这里还有一些其他的属性,比如LANGUAGE_CODE 和 TIME_ZONE,但是我们先把精力聚焦在重要的属性上。

We will later revisit this file and see how relevant it is in developing our site.
我们接下来会回顾这个文件,看看我们在开发中是如何使用它的。

Let’s next create our backend server!
接下来让我们开始开发我们的后台服务吧!