Model View Controller

From 118wiki

Jump to: navigation, search

Model View Controller

Django is just one of many frameworks built upon the Model-view-controller pattern.

A Software_pattern is a frequently recurring way of doing things when writing software. One of the main goals of modern software engineering is Code reuse, sometimes called the DRY (Do not Repeat Yourself) principle. By this principle, software patterns should be encoded once, perhaps expressed as classes with interfaces, and put into an API library so that they do not have to be written more than one time. Most software patterns need to be customized, and OO-programming languages usually provide some way to override methods and parameters, so that the patterns can be tweaked and customized. Unfortunately, many patterns go beyond what one Class or set of interfaces can provide; some design patterns are therefore expressed in a higher level way, informally or as a set of components (like Software_componentry).

The Model-view-controller pattern is often explained with a diagram showing three boxes in a triangular arrangement, with a box for the Model, a box for the View, and a box for the Controller, and arrows showing how these interact.

In Django, the Model corresponds to the definitions in models.py, which are used to define Classes for the modeled data objects and to generate backend SQL operations. The views.py file defines methods that fulfill the View part of the diagram (they present the responses to requests for viewing the model data). Django does the work of the Controller, by following the directions in the urls.py file and following procedures explained here. If you read all the design philosophy behind the Model-view-controller pattern, you will find that Django doesn't exactly follow this design pattern -- for instance, the views can update the model. Yet it is helpful to know the MVC pattern to understand Django's motivation.

Django goes further than the MVC pattern: the Generic Views feature of Django (see also this chapter) automates the presentation and update of simple objects in the model.

Personal tools