Introduction to using MVC design patterns in Python

Source: Internet
Author: User
I. Code organization (directory structure)

Ii. an overview of MVC

The MVC design pattern is the MVC framework.

The full name of MVC is the model View Controller, which is the abbreviation for models-View-Controllers (Controller), a software design paradigm that organizes code with a method of explicit separation of business logic and data. By aggregating business logic into a single component, the interface and user interaction around the data can be improved and customized without the need to rewrite the business logic. MVC is uniquely developed to map the traditional input, processing, and output functions in a logical graphical user interface structure.

Third, the Code

1. Data

#coding: Utf-8 ' simulation data, of course, this is a tuple. can also be any database, as long as you like ' Quotes = (' Live a bohemian life, easy like yielded, but to meet friends unlimited friends, but too hard. Balzac ',          ' The more books I read, the closer I get to the world, the more I understand the meaning of life, the more I feel the importance of life. --"          life is not like a train going through every stage of life." Life always walks straight ahead, never leaves anything behind. --Lewis ',          ' to always feel that the land of the motherland is firmly at your feet, to live with the collective, to remember, is to educate you collectively. If you break with the collective that day, that is the beginning of the end. -Ostrovsky ',          ' in school and in life, the most important motivation for work is the pleasure of work, the pleasure of working to get results, and the recognition of the social value of the results. --Albert Einstein ')

2. Model Layer
Only the model layer can access the data directly

#_ *_coding:utf-8from mvc.database.quote Import Quotes #导入数据  class Quotesmodel (object):    ' model layer '    def get _quote (self,index):        "' reading data from index        @parameter Index index value        '        try:            valve = Quotes[index]        except Indexerror as err:            valve = ' not found! '        return valve

3. Controller Layer

#_ *_coding:utf-8from Mvc.model.quote_model Import quotesmodelfrom mvc.view.quoteterminalview Import Quoteterminalview class Quoteterminalcontroller (object):    ' controller layer '    def __init__ (self):        Self.model = Quotesmodel ()        Self.view = Quoteterminalview ()     def run (self):        n = self.view.select_quote ()        try:            index = INT (n)            quote = self.model.get_quote (index)            self.view.show (quote)        except ValueError as ERR:            self.view.error (' Illegal index value ')

4. View Layer

#_ *_coding:utf-8 class Quoteterminalview (object):    "View Layer"    def show (self, quote): "'        display query result        @ Parameter quote receive Data '        print (' The famous quote you've queried is:%s '% (quote))     def error (self, msg): ' '        print error message        @msg msg Receive error message "        print (" Error:%s "% (msg))     def select_quote (self):        ' read the user's choice ' '        return Raw_input (" Please enter a number to inquire: ")

5. Main program

#_ *_coding:utf-8 ' main program ' from Mvc.controller.quoteterminalcontroller import Quoteterminalcontroller def mains (): While    True:        controller = Quoteterminalcontroller ()        controller.run () if __name__ = = ' __main__ ':    Mains ()
Related Article

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.