Early use 1Django framework
Speaking of the Django framework, certainly we need to first clear a concept that software framework. Here is the first question:
1 software framework (software framework)
1.1 The Definition
Software Framework: usually refers to in order to achieve an industry-standard or complete certain basic tasks of software component specification, but also for the achievement of a software component specification , provide the required specification of software products based features . 1
Software framework is a software product with basic functions:
- Basic functions: can be understood as business scenarios in order to meet certain set of functions.
- Software Products: Software framework in order for a certain type of software design problems that arise.
1.2 image understanding
- In fact, the software framework can be thought of as a company, there are various functional departments, each department carry out their duties, to complete the work by the cooperation between the sectors that will form a company's organizational structure.
Software framework, too, but said a company, it is tied to a market set up, the software framework is designed for a certain type of software problems and design, its main purpose is to improve the efficiency of software development .
Each frame is made of software modules, each module will have nowhere specific function. The development of mutual cooperation between a software module and the module to complete.
In the introduction to what software frame is after, we need to look at specific frame mode, introduce MVC framework mode here:
2 MVC
2.1 framework, design pattern, architecture
I was very troubled by this problem, we find a lot of relevant text, and experience for the next summary statement:
basic concepts:
Frame usually code reuse ;
design pattern is designed to reuse , which can be instantiated only after a code is represented;
frame is interposed therebetween , portions of code reuse, reuse part of the design, analysis may also be reusable. Software architect in the production of software, a kind of blueprint planning software, usually hierarchical, draw the relationship between the various components. 2
Compare:
- Framework and architecture: the architecture design somewhat, somewhat technical framework;
frame architecture and more particularly more than focusing on specific business scenario, a variety of architectures may be implemented by a frame.- Framework and Design Patterns: Design Patterns is smaller than the frame elements, more abstract;
a framework often contain one or more design patterns, frameworks, always for a particular application areas (such as Django is only for web development), but the same pattern was applied to a variety of applications. Both work together to reuse, so that thinking can learn from each other.- Architecture and Design Patterns:
Design mode is used to solve a specific problem, to a lesser extent; architecture design for the architecture category larger. It may appear multiple design patterns to result in a framework issue architecture.
Logical thinking in this order:
Doing a project, the first thing should be designed architecture, and then again consider the use of what frameworks and design patterns. But usually encountered are not particularly complex system, with a number of frameworks and design patterns is enough. 3
2.2 Past and Present
2.2.1 Past
(1) Model (template) -View (view) -Controller (controller)
- First is a software design pattern, to the conventional input (INPUT), processing (Processing), an output (output) applied to the graphical user task model designed to interact;
- Subsequently, MVC idea is applied in the development of the Web, known as Web MVC framework.
(2)
generate the MVC concept: the division of labor . Let a special person to do special things.
MVC core idea: decoupling . Let reduce coupling between different code blocks, the code to enhance portability and scalability, backward compatibility.
2.2.2 life
(1) web mvc FIG frame
(2) Specific Features
- M is a spelling Model: Main layer package access to the database, the data in the database to add, delete, change, search operation.
- V spelling of View: for packaging a result, content generated html page display.
- C is a spelling Controller: means for receiving a request, the business logic processing, Model View and interact with, the result returned.
After introduction relevant basic concepts, Hereinafter Django framework to understand the overall logic and specific processes:
3 Django overall logic
3.1 Introduction
- The main purpose is simple, rapid development of database-driven Web site
- Emphasize code reuse , multiple components can easily to "plug-in" as a service to the entire framework, Django has many powerful third-party plug-ins, you can even easily develop their own toolkit.
- It has strong scalability .
Django framework that follows the MVC design, and there is a proper noun: MVT
3.2 MVT
(1) Django MVT FIG frame
(2) Specific Features:
- M spelling to Model: the M functions in the same MVC is responsible for interacting with the database, data processing.
- V spelling of View: C with the same function in MVC, receives the request, performs service processing and returns a response.
- T全拼为Template:与MVC中的V功能相同,负责封装构造要返回的html。
4 Django项目的构建流程
4.1 搭建环境
问1:
如果在一台机器上,想开发多个不同的项目,需要用到同一个包的不同版本,如果还使用sudo pip3 install 包名称
的命令,在同一个目录下安装或者更新,其它的项目必须就无法运行了,怎么办呢?
答1:使用虚拟环境。
问2:
什么是虚拟环境?
答2:虚拟环境其实就是对真实pyhton环境的复制。
这样我们在复制的python环境中再去安装相应的包就不会影响到真实的python环境了。
通过建立多个虚拟环境,在不同的虚拟环境中开发项目就实现了项目之间的隔离。
4.1.1 虚拟环境安装
(1)首先安装虚拟环境,命令如下:
sudo pip3 install virtualenv #安装虚拟环境
(2)接下来还要安装虚拟环境扩展包,命令如下:
sudo pip3 install virtualenvwrapper #安装虚拟环境包装器的目的是使用更加简单的命令来管理虚拟环境。
(3)修改用户家目录下的隐藏配置文件.bashrc
,在文件最后出添加如下内容:
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh
(4)创建python3虚拟环境的命令如下:
mkvirtualenv -p python3 虚拟环境名称
例:
mkvirtualenv -p python3 test1_py3
综述:
创建成功后,会自动工作在这个虚拟环境上。
创建虚拟环境需要联网。
工作在虚拟环境上,提示符最前面会出现"(虚拟环境名称)"。
所有的虚拟环境,都位于/home/您的用户名/下的隐藏目录.virtualenvs下。
4.1.2 虚拟环境相关终端语句操作
- 退出虚拟环境:
deactivate
- 查看所有虚拟环境:
workon 两次tab键
- 使用虚拟环境:
workon 虚拟环境名称
删除虚拟环境:
rmvirtualenv 虚拟环境名称
例: 先退出:
deactivate
;再删除:rmvirtualenv py_django
在虚拟环境中可以使用pip命令操作python包:
pip install 包名称
注意:在虚拟环境中不可使用
sudo pip install 包名称
来安装python包
,这样安装的包实际是安装在了真实的主机环境上。查看已安装的python包:
pip list
orpip freeze
这两个命令都可已查看当前工作的虚拟环境中安装了哪些python包,只是显示的格式稍有不同。
安装django包:
pip install django==1.8.2
如果前面删除过虚拟环境py_django,则需要先创建一下,否则直接安装django包即可。
4.1.3 自我总结
- 在为了避免在正式环境下创建多个项目而导致相关安装包的冲突,从而进行虚拟环境安装后,我们就可以在我们安装的的虚拟环境中进行Django项目的创建了。
逻辑关系上是我们可以根据我们的需求创建多个虚拟环境,而在每个虚拟环境下我们就可以进行相关Django项目的创建
在不同的虚拟环境下我们可以自由的根据该虚拟环境下Django项目的需要进行相关包的安装,这样就解决了前文所说的同一个包不同版本之间的替代问题。
下面就介绍下Django项目的创建:
4.2 创建Django项目
强调一下,创建Django项目前:
一是要在正确的虚拟环境中间(即要进入4.1步骤中已经创建好的虚拟环境中);
二是方便后期相关文件管理,我们最好创建该项目的专属文件夹。
4.2.1 创建Django的命令
创建项目的命令如下:
django-admin startproject 项目名称
例:
django-admin startproject test1
4.2.2 Django项目默认目录说明
进入4.2.1创建的Django项目test1目录,查看目录树形结构
manage.py
是项目管理文件,通过它管理项目。- 与项目同名的目录,此处为
test1
。 _init_.py
是一个空文件,作用是这个目录test1可以被当作包使用。settings.py
是项目的整体配置文件。urls.py
是项目的URL配置文件。wsgi.py
是项目与WSGI兼容的Web服务器入口。
在django中,项目的组织结构为一个项目包含多个应用,一个应用对应一个业务模块。下面对应用进行介绍:
4.3 创建Django项目下的应用
Django中对于应用的操作分为创建和安装,下面分别介绍:
4.3.1 应用的创建
创建应用的命令如下:
python manage.py startapp 应用名
例如:
python manage.py startapp test_app
应用默认目录说明:
_init.py_
是一个空文件,表示当前目录booktest可以当作一个python包使用。tests.py
文件用于开发测试用例,在实际开发中会有专门的测试人员,这个事情不需要我们来做。models.py
文件跟数据库操作相关。views.py
文件跟接收浏览器请求,进行处理,与M和T进行交互,返回页面相,定义处理视图函数。admin.py
文件跟网站的后台管理相关。migrations
文件夹之后给大家介绍。
应用创建成功后,需要安装才可以使用,也就是建立应用和项目之间的关联;
4.3.2 应用的安装
建立应用和项目之间的联系,需要对应用进行注册。
修改settings.py
中的INSTALLED_APPS
配置项。
4.4 运行服务器
通过上面的一些了操作,就可以使用django提供的一个纯python编写的轻量级web服务器,仅在开发阶段使用。
运行服务器命令如下:
python manage.py runserver ip:端口
例:
python manage.py runserver
You can not write IP and port, the default IP is 127.0.0.1, the default port of 8000.