this article illustrates the MVC pattern of the Python design pattern. Share to you for your reference, as follows:

mvc the Model-View-Controller pattern

mvc model model is the one used in design patterns in software engineering. The MVC pattern is detached from the previous simple web service design logic that separates development, testing, and maintenance. In the MVC pattern, applications are decomposed into interacting modules, models, views, and controls. The purpose is to separate input (control), processing logic (model), and output format (view).

simple to understand:

1. control module is used for acquiring user input, and the model with the view to establish contact
2. model is the main access to data from the
memory area 3. views are used to show the user data from the

alt= model for "src=" 201801010508 ">

: control module can be viewed as a range of users, processing (model), display (view) intermediate between people. It is the entrance to the user's request and the application processing. The control module accepts user input and parse to decide which model and view participate in the processing, so it decides what view and model to select for user request.

model module: processing business applications, model operating database, such as inserts, updates, deletions. Each model will provide a fixed type of data to the control module, on the other hand, different data processing method invocation model can control module, and the processed results are returned to the

: view module is mainly used to display, through the control module to obtain model data. Display and formatting. The view is selected by the control module and the feedback is given to the user. The choice of the view model is based on the L selection and user configuration of the model module and so on.

. Two examples of testing a simple management system to query the error list

Scene Description:

if the user query a specific error, test management system shows that the description of the error if the
key value search related errors in some format, display all the test management system the error list

to create SQLite database, TMS library, and create a table of


ID Component< /td>

1 XYZ File doesn't get deleted
2 XYZ Registry doesn't get created
3 Wrong Title gets displayed import code as follows: 

sqlite4 import types class DefectModel: def getDefectList (self, component): query = "select ID from defects where Component='%s'defectlist% component = self._dbselect (query) list for row in defectlist: list.append [] = (row[0]) return (self list def getSummary, ID): query =" select summary from defects where ID='%d' summary% id = self._dbselect (query) for row in summary: return row[0] def _dbselect (self, query): connection = sqlite3.connect ('TMS') cursorObj = connection.cursor (results) = cursorObj.execute (query) connection.commi (T) cursorObj.close (return) results class DefectView: def summary (self, summary, defectid): "print Defect Summary for defect#%d####%sn ####% (defectid, summary) def defectList (self, list, category):" print Defect List for%s ####n ####% category for defect in list: "print defect class Controller: def __init__ (self): pass def getDefectSummary (self, defectid): model = DefectModel (view) = DefectView (summary_data) = model.getSummary (defectid) return view.summary (summary_data, defectid) def getDefectList (self, component): model = DefectModel (view) = DefectView (defectlist_data) = model.getDefectList (component) return view.defectList (defectlist_data, component)

 import MVC module: controller = mvc.Controller (print) controller.getDefectSummary (2) print controller.getDefectList ('ABC') 

MVC summary: through this design method, see decoupling benefits, each module independent, do not affect each other, can also increase the module. Convenient combination, easy to disassemble. Have a good experience!

more about Python related content can see the special station: "Python data structure and algorithm tutorial " and "Python Socket ", summarizes the programming skills of "Python" /a> "function to use skills summary," Python string manipulation skills summary "and" Python entry and advanced tutorial classic "

to the Python program designed to help all of you.

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:A simple example of the MVC pattern of the Python design pattern | Script Home

You may also be interested in these articles!