Web frameworks and MVC, MTV

Source: Internet
Author: User

Summary of the content of this article
----The original web framework----custom web framework----MVC and MTV
I. The nature of the web framework

For all Web applications, essentially a socket server, the user's browser is a socket client

Simply put, the socket passes in the data, followed by the business logic handler

It's not a good picture.

Ii. introduction of Wsgi

Because the web framework consists of two parts 1, Socket2, logical processing some frameworks have only logical processing, there are no sockets, such as flask, bottle, Django some framework two parts, such as tornado those without sockets, Need to write or introduce a third-party socket, because there is no fixed standard, so more chaotic, this time Wsgi on the grand debut,
Chinese Translation Web Service Gateway Interface
WSGI (Web server Gateway Interface) is a specification that defines the interface format between Web apps and Web servers written using Python.
Enable decoupling between Web apps and Web servers.

The standalone WSGI server provided by the Python standard library is called Wsgiref. When there are other interface formats:

'CGI': Cgiserver,'Flup': Flupfcgiserver,'Wsgiref': Wsgirefserver,'Waitress': Waitressserver,'CherryPy': Cherrypyserver,'Paste': Pasteserver,'FAPWS3': Fapwsserver,'Tornado': Tornadoserver,'Gae': Appengineserver,'Twisted': Twistedserver,'Diesel': Dieselserver,'Meinheld': Meinheldserver,'Gunicorn': Gunicornserver,'Eventlet': Eventletserver,'gevent': Geventserver,'Geventsocketio': Geventsocketioserver,'Rocket': Rocketserver,'Bjoern': Bjoernserver,'Auto': Autoserver,
View Code

Django-Implemented Wsgiref:

#!/usr/bin/Env Python#coding:utf-8   fromwsgiref.simple_server Import make_server def runserver (environ, start_response): Start_response ('OK', [('Content-type','text/html')])    return ''  if__name__ = ='__main__': httpd= Make_server ("',8000, runserver) print"serving HTTP on port 8000 ..."Httpd.serve_forever ()
View Code

Parameters of the above template engine

The first parameter, Environ, represents all requests for the encapsulated user

The second parameter, Start_response, represents the returned data

III. Customizing the web framework

The directory structure is as follows:, the view file package is an HTML file

Controllar file is

#/usr/bin/Env python#-*-coding:utf-8-*-Import OsdefNew(): F=open (Os.path.join (" views","s1.html"),"R") Data=F.read () f.close ( )returnDatadef Index (): F=open (Os.path.join (" views","index.html"),"R") Data=F.read () f.close ( )returnData
View Code

The start file is:

#/bin/usr/Env python#-*-coding:utf-8-*- fromwsgiref.simple_server Import make_server# fromURLs Import urlsimport controllardef runserver (environ,start_response): Start_response ('OK',[('Content-type','text/html')]) URL=environ['Path_info']    ifurl=="/new": Ret=controllar.New() elif URL=="/index": Ret=Controllar.index ()Else: Ret="404"    returnRET #ifUrlinchUrls.keys (): # Func_name=Urls[url] # ret=func_name () #Else: # ret=404    # returnretif__name__=="__main__": httpd=make_server ("",Bayi, Runserver) httpd.serve_forever ()
View Code

The above files correspond to, respectively, the HTML and the function, and the socket

But the above code can only be returned to the user HTML content, but cannot return the dynamic content, how to solve? Post-decomposition

Four, the MVC pattern:

The so-called MVC is to divide Web applications into models (m), controllers (C) and view (V) Three layers, which are joined together in a plug-in, loosely coupled way: M. model is responsible for mapping (ORM) of business objects to databases, database-related Operations v. View is responsible for interacting with the user (page) (template HTML file) c. Controller accepts user input call model and view complete user's request (business logic)

V. MTV Mode

The Django MTV model is essentially the same as MVC, and also for the sake of maintaining a loosely coupled relationship between the components, just the definition of a slightly different Django MTV is the value:
    • M represents the model: a relational mapping (ORM) that is responsible for business objects and databases.
    • T stands for Template: is responsible for how to display the page to the user (HTML).
    • V represents the View: Responsible for business logic and calls model and template at the appropriate time.
In addition to the above three layers, a URL Dispatcher, it is the role of the URL of the page request distribution to different view processing, the view then call the corresponding model and TEMPLATE,MTV response mode as follows:
    1. Web server (middleware) receives an HTTP request
    2. Django finds the corresponding view function in urlconf to handle HTTP requests
    3. The view function invokes the corresponding data model to access the data and to invoke the corresponding template to present the page to the user
    4. The view function returns an HTTP response to the Web server after processing ends
    5. The Web server sends the response to the client

The key advantage of this design pattern is that the various components are loosely coupled. Thus, each Django-driven Web application has a clear purpose and can be changed independently without affecting other parts.
For example, a developer changes a URL in an application without affecting the underlying implementation of the program. Designers can change the style of the HTML page without touching the Python code.
The database administrator can rename the data tables and simply change the model without needing to find and replace them from a large stack of files. The Python file for the Django MTV model is as follows:

If you do not consider the internal principle of a sentence summed up MVC and MTV:MTV---MTV Framework is the destination of the folder

Summarize:

1 ,    plug-in 2,    the essence of the Web framework Web frame is the socket, send the string through the string: the    first block: The protocol and the way the    second block: The request header    Third block: Send content Hello response string:         the second block: Response header        Third block: Response content "Hello" Python   Web Framework Classification:    Self-sufficiency           : socket= ="Tornado    third party: WSGI+ Framework MVC.MTV        folder collation
View Code

Web frameworks and MVC, MTV

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.