Introduction to mvc design patterns in python

Source: Internet
Author: User
This article shares the introduction of using the mvc design pattern in python. I. code organization (directory structure)

II. mvc overview

The MVC design pattern is the MVC framework.

The full name of MVC is Model View Controller, short for model-view-controller. it is a Model of software design, organize code by explicitly separating the business logic and data, and aggregate the business logic into a component, the interface and user interaction around data can be improved and customized without re-writing the business logic. MVC is uniquely developed to map traditional input, processing, and output functions in a logical graphical user interface structure.

III. code

1. Data

# Coding: UTF-8 ''' simulate data. of course, this is a tuples. It can also be any database, as long as you like '''quotes = ('if you have a loose life, it is easy to get along with the boat, but it is difficult to get to know friends and friends. -- Balzack': 'The more books I read, the closer I get to the world, the clearer the meaning of life, and the more important I feel. -- Gorky ', 'The Life does not go through every stage of life like a train. Life always walks forward and never leaves anything. -- Lewis ',' always think that the land of the motherland is firmly under your feet. to live with the collective, remember that it is collective education for you. If you break away from the group on that day, it is the beginning of the last line. -- Ostrovs', 'The most important motivation for work in school and life is the pleasure in work, the pleasure in work results, and the awareness of the social value of these results. -- Einstein ')

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

# _ * _ Coding: utf-8from mvc. database. quote import Quotes # import data class QuotesModel (object): ''' model layer ''' def get_quote (self, index ): ''' read data based on the index @ parameter index value ''' try: valve = Quotes [index] cannot 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) comment T ValueError as err: self. view. error ('invalid 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 metric you have queried is famous: % s' % (quote) def error (self, msg ): ''' print error message @ msg receive error message ''' print ("error: % s" % (msg) def select_quote (self ): '''select to read users' '''return raw_input ("Enter the number to query :")

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 ()

The above describes how to use the mvc design pattern in python. For more information, see other related articles in the first PHP community!

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.