Django 简明教程

Django - Apps Life Cycle

项目是许多应用程序的总和。每个应用程序都有一个目标,可以重复使用到其他项目中,例如网站上的联系表单可以是一个应用程序,可以重复使用到其他网站。将其视为您项目的模块。

A project is a sum of many applications. Every application has an objective and can be reused into another project, like the contact form on a website can be an application, and can be reused for others. See it as a module of your project.

Create an Application

我们假设您在您的项目文件夹中。在我们的主“myproject”文件夹中,然后管理。py -

We assume you are in your project folder. In our main “myproject” folder, the same folder then manage.py −

$ python manage.py startapp myapp

您刚刚创建了 myapp 应用程序,和项目一样,Django 使用应用程序结构创建一个“myapp”文件夹 -

You just created myapp application and like project, Django create a “myapp” folder with the application structure −

myapp/
   __init__.py
   admin.py
   models.py
   tests.py
   views.py
  1. init.py − Just to make sure python handles this folder as a package.

  2. admin.py − This file helps you make the app modifiable in the admin interface.

  3. models.py − This is where all the application models are stored.

  4. tests.py − This is where your unit tests are.

  5. views.py − This is where your application views are.

Get the Project to Know About Your Application

在此阶段,我们有了“myapp”应用程序,现在我们需要将其注册到 Django 项目“myproject”中。为此,请更新项目的 settings.py 文件中的 INSTALLED_APPS 元组(添加您的应用程序名称) -

At this stage we have our "myapp" application, now we need to register it with our Django project "myproject". To do so, update INSTALLED_APPS tuple in the settings.py file of your project (add your app name) −

INSTALLED_APPS = (
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'myapp',
)