Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC pattern in django

This issue has been tormenting me for a while already. I've read about this topic but nothing seems to clear my thoughts. I understand that they call the views templates, and the models models as well, what I don't really get is where the controllers are. What django calls views seem to me more like actions/methods/functions of a controller than a controller itself, but anywhere I read, I find that supposed view-controller equivalency.

I've worked with MVC frameworks before (ASP.NET MVC3, Ruby on Rails, PHP Laravel Framework), and they all define the controllers as the same thing: a bunch of functions related to a specific topic of the site, namely user accounts or something like that. The best equivalency that I find between this description and django features are the apps, but of course I'm wrong due to the huge amount of people and documentation going the other way.

Could anybody help me with this? Does my mindset make any sense? Am I missing something essential here and then I can't get these concepts right?

like image 371
ecampver Avatar asked Sep 09 '13 03:09

ecampver


3 Answers

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

like image 110
Daniel Roseman Avatar answered Sep 28 '22 14:09

Daniel Roseman


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.

like image 41
Henrik Andersson Avatar answered Sep 28 '22 14:09

Henrik Andersson


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

like image 28
user4673824 Avatar answered Sep 28 '22 15:09

user4673824