The second: "empty," the blog application

Involved in the text? Sample code, has been updated to synchronize HelloGitHub-Team warehouse

A blog application

We have established a blog django project engineering, and successfully run it. But so far all this is just the initial content django project we created, can not be django generate initialization code for our blog, these features of the code had to write our own.

Django encourages us to organize the code I have written to the application (Application) years, and the best is an application to provide only one function. For example django blog, we want to develop the relevant code of the application are placed in the blog. In fact, applications are nothing special, but the function is to organize related code into a folder, this folder has become an application (tentatively it can be understood).

We can manually build in a django project directory folder for it a name, and told that this is a django application. But we do not have to do this boring job, django has provided to automatically create application command for us.

Now django blog to create our application, I named it for the blog. Manage.py file into the directory where (ie project root directory), run the pipenv run python manage.py startapp blogcommand to create a blog application:

> pipenv run python manage.py startapp blog

As already said, manage.py is our project management assistant. In a previous tutorial we used it in runserverorder to open up a local development server, where we use the manage.py startappcommand to create a blog application.

The directory structure of the application

Open with Explorer directory manage.py file is located, you can see more than a blog directory, the directory file structure below as follows:

blog\
    __init__.py
    admin.py
    apps.py
    migrations\
        __init__.py
    models.py
    tests.py
    views.py

Files with different names for storing code for a specific function, which will be described in detail later. In short this django application folder structure has been created for us, but it is only a file that contains a variety of file folders only, django is not known it is an application. We have to tell django This is something we build applications, said professional is registered in the django application configuration file.

Open the settings.py file under HelloDjango-blog-tutorial directory, see the name to know settings.py is a settings file (setting means setting), to find INSTALLED_APPSsettings, add into the blog application.

HelloDjango-blog-tutorial/blogproject/settings.py

## 其他配置项...

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog', # 注册 blog 应用
]

## 其他配置项...

You can see django has provided some built-in applications for us, these applications provide a variety of functions. This is a powerful place django, django generic functions are written to help us, we only have to write code to function associated with their businesses.

Weekly complimentary two countries destined for django tickets! From a zero base to take you step by step to fly, reach forward this free tickets to your little friends now.

Guess you like

Origin www.cnblogs.com/xueweihan/p/11249515.html