Friday, October 10, 2008

Why Django is not a pure MVC framework

In the acronym jungle of web development, much has been made of the Model-View-Controller (MVC) design pattern.   It's lauded as one of Ruby on Rails' strengths and has clearly influenced Django's design.
Django's creators, Holovaty and Kaplan-Moss, write in Chapter 1 of The Definitive Guide to Django that:
Simply put, MVC defines a way of developing software so that the code for defining and accessing data (the model) is separate from request routing logic (the controller), which in turn is separate from the user interface (the view).
This didn't make intuitive sense to me  -- shouldn't the "user interface" be considered the "controller"?  And there's no mention of templates here at all-- a fundamental component of Django.  Those seemed most naturally part of the "view" in Django. 

Looking for some clarity, I went back and read a highly-cited 1988 paper by Glenn Krasner and Stephen Pope (where the above image derives from), entitled "A cookbook for using the model-view controller user interface paradigm in Smalltalk-80", where they describe the MVC concept as it was originally implemented.   They write:
Models
The model of an application is the domain-specific software simulation or implementation of the application's central structure.  This can be as simple as an integer (as the model of a counter) or string (as the model of a text editor), or it can be a complex object that is an instance of a subclass [...]

Views
In this metaphor, views deal with everything graphical; they request data from their model, and display the data. [...]

Controllers
Controllers contain the interface between their associated models and views and the input devices (e.g. keyboard, pointing device, time).  Controllers also deal with scheduling interactions with other view-controller pairs [...].
A-ha!  This makes more sense.  The views handle how data looks, and the user interface is the controller -- in contrast to what Django's creators stated above.  

However, thinking I had stumped the Django folks, I came across this concession in Chapter 5 of The Definitive Guide to Django:
If you’re familiar with other MVC Web-development frameworks, such as Ruby on Rails, you may consider Django views to be the “controllers” and Django templates to be the “views.” 
Indeed, I think this is an unfortunate accident, a minor (yet confusing) sin of semantics on Django's part.  Quite clearly MVC controllers are Django views, and MVC views are Django templates.  This understanding (and ordering) is reflected in the acronym "MTV" -- Model-Template-View -- that some have used to describe Django.

2 comments:

Anonymous said...

Hi Mike -- If you'd like to learn more about the MVC framework, it's covered in excellent detail in a book called 'Design Patterns' -- by Erich Gamma, Richard Helm, Ralph Johnson, and John M. Vlissides.

Joe

Unknown said...

Actually Django is more of 3-tier architecture rather than MVC.

The Template - or call should we call it "View" for clarity is communicating only with "Controller"(View).

MVC is just a fancy name applied to everything nowadays. While the core feature to justify is ... conectivity between all 3 only then its MVC.

If everything goes through in linear way then its n-tier/multitier arch.

In django for instance:
You call an url /mysite/somthing/.
Url gets resolved and proper view is executed, the view asks model to give him some data, data returns view wraps the data and passes it along to template(view), which than presents data accordingly.

In rails you can have data from both controller and view.