123

Can somebody explain me where the diferences are between Django and the Model View Controller pattern?

Functionally, what can we expect from those differences — i.e. what works differently comparing Django to, for example, Ruby on Rails?

Leonardo
  • 2,484
  • 2
  • 25
  • 37

4 Answers4

150

According to the Django Book, Django follows the MVC pattern closely enough to be called an MVC framework.

Django has been referred to as an MTV framework because the controller is handled by the framework itself and most of the excitement happens in models, templates and views.

You can read more about MTV / MVC here:

The MTV (or MVC) Development Pattern

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.

This is an unfortunate confusion brought about by differing interpretations of MVC.

In Django’s interpretation of MVC, the view describes the data that gets presented to the user; it’s not necessarily just how the data looks, but which data is presented.

In contrast, Ruby on Rails and similar frameworks suggest that the controller’s job includes deciding which data gets presented to the user, whereas the view is strictly how the data looks, not which data is presented.

Community
  • 1
  • 1
Paolo Moretti
  • 54,162
  • 23
  • 101
  • 92
  • 7
    Thanks for the great answer. Coming from Rails to Django, this answers one of the things I found most frustrating: why does django put the controller code in a file called views.py!? – dgmdan Jul 27 '13 at 14:37
  • @dgmdan It's just a default convention, you can choose the name you like. But I agree, it seems weird :) – Paolo Moretti Jul 28 '13 at 17:13
  • 1
    I would rather leave views.py as a view layer and create a set of controller classes into a controller package to avoid Django's confusion. – stanleyxu2005 Aug 18 '13 at 01:28
  • 8
    @dgmda: Becuase the concept of a "controller" is a misnomer in webapps. MVC is an event driven framework that doesn't fit only HTTP's stateless REQUEST/RESPONSE model. It shouldn't be called MVC in the first place. Almost all webapps are not MVC, but uses a model and a function or class usually called a View. The View in turn can delegate the HTML rendering to a Template, but doesn't need to. So there isn't a controller, actually. – Lennart Regebro Nov 05 '13 at 12:05
  • 2
    I back Lennart Regebro's concept that a stateless model like HTTP shouldn't have a controller and an MVC model for webapps just brings about major confusion – Hussam Oct 28 '14 at 08:38
  • Can this be considered as the reverse MVC pattern? – isnvi23h4 Sep 27 '15 at 11:42
  • As others have commented, Django's alternate terminology is quite confusing when you start using it. Thanks for clarifying. This should be the accepted answer. – Alan Evangelista Apr 29 '16 at 22:08
26

The Django FAQ itself is a decent place to start:

In our interpretation of MVC, the “view” describes the data that gets presented to the user. It’s not necessarily how the data looks, but which data is presented. The view describes which data you see, not how you see it. It’s a subtle distinction.

...

Furthermore, it’s sensible to separate content from presentation – which is where templates come in. In Django, a “view” describes which data is presented, but a view normally delegates to a template, which describes how the data is presented.

Where does the “controller” fit in, then? In Django’s case, it’s probably the framework itself: the machinery that sends a request to the appropriate view, according to the Django URL configuration.

If you’re hungry for acronyms, you might say that Django is a “MTV” framework – that is, “model”, “template”, and “view.” That breakdown makes much more sense.

Bear in mind that “Model View Controller” is just a pattern, i.e. an attempt to describe a common architecture. So a better question might be “How well does Django fit the Model View Controller pattern?”

Community
  • 1
  • 1
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
11

When you code, not thinking about names of framework pieces, there are no susbtantive differences betw, for example RoR. But it depends on the use you give models, since on Django they easily contain some logic that on other frameworks would stay at controller level.

The view on Django tends to be a set of queries for fetching data, and pass them to the template.

inigomedina
  • 1,791
  • 14
  • 21
7

In mvt, a request to a URL is dispatched to a View. This View calls into the Model, performs manipulations and prepares data for output. The data is passed to a Template that is rendered an emitted as a response. ideally in web frameworks, the controller is hidden from view.

This is where the difference is from MVC: in mvc, the user interacts with the gui, the controller handles the request and notifies the model and the view queries the model to display the result to the user.

Samuele Mattiuzzo
  • 10,760
  • 5
  • 39
  • 63