jango Introduction Introduction to Django

Introduction to Django

 

 Django framework Introduction

MVC framework and MTV framework

The MVC, full name Model View Controller, software engineering, a software architecture model, the software is divided into three basic parts: the model (the Model), view (View) and a controller (the Controller), coupled with low , high reusability, life cycle and low cost.

For a more detailed understanding of the MVC pattern? >> point I

Django framework adopts the idea of ​​MVC design pattern frame is divided into three parts, to reduce the coupling between the various parts.

Django framework differs in that it is split into three parts: Model (model), Template (templates) and View (View), i.e. MTV frame.

Django's MTV mode

       Model (model): Responsible for business objects and database objects (ORM)

       Template (template): responsible for how the page displayed to the user

       View (View): is responsible for the business logic, and calls the Model and Template at the appropriate time

In addition, Django urls as well as a distributor, it is the role of a URL of a page request distributed to different view process, view and then call the appropriate Model and Template

Django framework shown

Django common commands

You need to use the command make some operations in Django Django's use, such as creating Django project, start the Django program, create a new APP, database migration.

Create a Django project

We have a new folder to store the project file, switch to this directory, start a command-line tool. Create a Django project called mysite of:

django-admin startproject mysite

After creating the project, you can view the current more than a file folder called mysite directory, mysite folder directory structure is as follows:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

启动Django项目

 启动项目的时候,需要切换到mysite目录下,执行如下命令:

python manage.py runserver  #默认使用8000端口

 命令后面还可以指定参数:

python manage.py runserver 8888  #8888为新指定的端口
python manage.py runserver 127.0.0.1:8000  #还可以指定IP和端口,冒号分割

创建APP

一个Django项目可以分为很多个APP,用来隔离不同功能模块的代码。

命令行创建

python manage.py startapp app01

执行命令后,项目目录下多出一个app01的文件夹,目录结构如下:

app01/
    migrations
        __init__.py
    __init__.py
    admin.py
    apps.py
    models.py
    tests.py
    views.py

使用PyCharm创建

可以使用PyCharm的manage.py工具来执行命名。在主菜单栏中选择Tools,在下拉菜单中选择Run manage.py task,会出现如下图所示的工具对话框:

在弹出的命令窗口中直接输入下面的命令便可创建app:

startapp app01

使用PyCharm的manage.py工具执行命令时,只用输入命令及参数即可,不再输入python manage.py了。

数据库迁移

python manage.py makemigrations
python manage.py migrate

创建超级用户

python manage.py createsuperuser

输入以上命令后,根据提示输入用户名、邮箱、密码、确认密码。密码的要求至少是不八位,不能和邮箱太接近,两次密码需要一致。

模板

Django模板(Template)系统 >> 点我

视图系统

Django视图系统 >> 点我

路由系统 

Django路由系统 >> 点我

模型

Django模型(model)系统 >> 点我

 
 
 

 Django框架简介

MVC框架和MTV框架

MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model)、视图(View)和控制器(Controller),具有耦合性低、重用性高、生命周期成本低等优点。

想要更详细的了解MVC模式? >> 点我

Django框架的设计模式借鉴了MVC框架的思想,也是分成三部分,来降低各个部分之间的耦合性。

Django框架的不同之处在于它拆分的三部分为:Model(模型)、Template(模板)和View(视图),也就是MTV框架。

Django的MTV模式

       Model(模型):负责业务对象与数据库的对象(ORM)

       Template(模版):负责如何把页面展示给用户

       View(视图):负责业务逻辑,并在适当的时候调用Model和Template

此外,Django还有一个urls分发器,它的作用是将一个个URL的页面请求分发给不同的view处理,view再调用相应的Model和Template

Django框架图示

Django常见命令

在Django的使用过程中需要使用命令让Django进行一些操作,例如创建Django项目、启动Django程序、创建新的APP、数据库迁移等。

创建Django项目

一把我们都新建一个文件夹来存放项目文件,切换到这个目录下,启动命令行工具。创建一个名为mysite的Django项目:

django-admin startproject mysite

创建好项目之后,可以查看当前目录下多出一个名为mysite的文件夹,mysite的文件夹目录结构如下:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

启动Django项目

 启动项目的时候,需要切换到mysite目录下,执行如下命令:

python manage.py runserver  #默认使用8000端口

 命令后面还可以指定参数:

python manage.py runserver 8888  #8888为新指定的端口
python manage.py runserver 127.0.0.1:8000  #还可以指定IP和端口,冒号分割

创建APP

一个Django项目可以分为很多个APP,用来隔离不同功能模块的代码。

命令行创建

python manage.py startapp app01

执行命令后,项目目录下多出一个app01的文件夹,目录结构如下:

app01/
    migrations
        __init__.py
    __init__.py
    admin.py
    apps.py
    models.py
    tests.py
    views.py

使用PyCharm创建

可以使用PyCharm的manage.py工具来执行命名。在主菜单栏中选择Tools,在下拉菜单中选择Run manage.py task,会出现如下图所示的工具对话框:

在弹出的命令窗口中直接输入下面的命令便可创建app:

startapp app01

使用PyCharm的manage.py工具执行命令时,只用输入命令及参数即可,不再输入python manage.py了。

数据库迁移

python manage.py makemigrations
python manage.py migrate

创建超级用户

python manage.py createsuperuser

输入以上命令后,根据提示输入用户名、邮箱、密码、确认密码。密码的要求至少是不八位,不能和邮箱太接近,两次密码需要一致。

模板

Django模板(Template)系统 >> 点我

视图系统

Django视图系统 >> 点我

路由系统 

Django路由系统 >> 点我

模型

Django模型(model)系统 >> 点我

Guess you like

Origin www.cnblogs.com/taosiyu/p/11260016.html