MVC pattern in django MVC pattern in django django django

MVC pattern in django


It's a mistake to think of design patterns like MVC as unbreakable rules. They really aren't: there are all sorts of ways of implementing them, which comply with the description to a greater or lesser extent.

This is especially the case in Python, where one of the guiding principles is "practicality beats purity" - in other words, do what works.

In any case, Django makes no claim to be an MVC framework. On the contrary, the documentation describes it as MTV: model, template, view. After all, outside the world of design patterns everyone calls "an HTML file with syntax for variables and flow control" a template, not a view.

(That FAQ entry also offers a possible answer to your question: the controller is the framework itself. But it goes on to emphasise that it's a mistake to try to shoehorn into these definitions.)


The views.py defines the view functions for your app and your app groups related functions together.

However what I believe you're missing here is the urls.py.The urls file is the first part of the controller.

The URL patterns inside urls.py dictates what you're allowed to pass in to the view function or view class (depending on what approach you took, either with function based views or class based views) and then routes it to the proper view function.

So there's tight a coupling between the views.py and the urls.py, that makes up for the entire Controller part of MVC.

Comparing it to Rails, would be that the urls.py is a routes.rb and the actual controller class is the views.py function/cbv.


The mismatch of terminology is unfortunate but more or less doing the same thing.