Django framework (a): MVC design pattern, Django Profile

1. MVC design pattern

MVC design pattern: Model-View-Controller shorthand.

Was first proposed by TrygveReenskaug in 1978, is the Xerox Palo Alto Research Center (Xerox PARC) a software design pattern in the 1980s as a programming language Smalltalk invention is to traditional input (input), processing (processing ), output (output) task to use graphical user interaction model and design. With the advent of standard input and output devices, developers only need to focus on the analysis and implementation of business logic. It came to be recommended as Oracle's Sun's Java EE platform design patterns, and welcomed by more and more use of ColdFusion and PHP developers. Although now no longer use the original division of labor, but this division is thought to be the standing, widely used in software engineering, is a typical and widely used software architecture model. Later, MVC idea is applied in the development of the Web, known as Web MVC framework.

MVC is commonly used in software engineering, software architecture model, which is a method for designing business logic and display interface separation. It is the software system is divided into three basic sectors: model (Model), view (View) and controller (Controller).

  • Controller Controller: to process the request, is responsible for forwarding the request.
  • View View: interface designer graphical interface design.
  • Model Model: write function (algorithm, etc.) program application, database management.

MVC framework is the core idea: decoupling, so reduce the coupling between different code blocks of code to enhance the scalability and portability, for backward compatibility.

The current mainstream of development languages ​​such as Java, PHP, Python in both MVC framework.

2. Django Profile

2.1 MTV design patterns

The MTV Django nature and MVC pattern is the same, but also in order to maintain the relationship between loosely coupled components, but slightly different definition, the MTV Django values ​​are:

  • M represents the model (Model): responsible for the business object-relational mapping and database (ORM).
  • T represents the template (Template): responsible for how to display a page to the user (html).
  • V represents the view (View) : is responsible for the business logic, and calls the Model and Template at the appropriate time.

In addition to the above three, but also requires a URL distributor, the role of which is a URL of a page request process distributed to different View, View, and then invokes the appropriate Model Template, MTV response mode is as follows:

 Generally user initiates a request (request) to our servers through a browser, the request back to access the view function, (if not related to the data call, so this time the view function returns a template is a web page to the user), view function call model, the model database to find data, and then back step, view the function returns the data to fill in the blank template, the last page is returned to the user.

2.2 Django's simple to use

2.2.1 Django installation

pip install django -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

2.2.2 Creating a Django project

We in the most common electronic business site, for example, there are now user-related pages on the site, there is a page with related merchandise, as well as with the relevant orders of the page, such as a site's content is actually a function module.

In django, the tissue structure of the project is a project comprises a plurality of applications, an application corresponding to a service module.

Create project command is as follows:

django-admin startproject 项目名称

Create a project, the project is named djangotest.

django-admin startproject djangotest

Then we look at the directory djangotest.

2.2.3 Description Item default directory

manage.py is a project management file, through its management of the project. 

Then is the directory project of the same name, here djangotest.

  • _init_.py is an empty file, this role is test1 directory can be used as packages.
  • asgi.py is the project's asgi profile.
  • settings.py is the project's overall profile.
  • urls.py is the project's URL configuration file.
  • wsgi.py是项目与WSGI兼容的Web服务器入口。

2.2.4 创建应用

使用一个应用开发一个业务模块,此处创建应用名称为booktest,完成图书的信息维护。

python manage.py startapp booktest

  • _init.py_是一个空文件,表示当前目录booktest可以当作一个python包使用。
  • tests.py文件用于开发测试用例,在实际开发中会有专门的测试人员,这个事情不需要我们来做。
  • models.py文件跟数据库操作相关。
  • views.py文件跟接收浏览器请求,进行处理,返回页面相关。
  • admin.py文件跟网站的后台管理相关。
  • apps文件夹是django1.10之后增加的,通常里面包含对应用的配置。

2.2.5 安装应用 

应用创建成功后,需要安装才可以使用,也就是建立应用和项目之间的关联,在djangotest/settings.py中INSTALLED_APPS下添加应用的名称就可以完成安装。 

初始项目的INSTALLED_APPS:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

接下来在元组中添加一个新的项,当前示例为booktest:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'booktest',
]

2.2.6 开发服务器

在开发阶段,为了能够快速预览到开发的效果,django提供了一个纯python编写的轻量级web服务器,仅在开发阶段使用。

运行服务器命令:

python manage.py runserver ip:端口

可以不写IP和端口,默认IP是127.0.0.1,默认端口为8000。

服务器启动成功:

紧接着在浏览器中输入网址“127.0.0.1:8000”,可以查看当前站点开发效果。

如果增加、修改、删除文件,服务器会自动重启;
按ctrl+c停止服务器。

Guess you like

Origin www.cnblogs.com/liuhui0308/p/12189658.html