Django explained with model/view/controller (MVC)

Tags: django, book

In an early chapter of my upcoming “solid Django” book, I have to explain Django. How it works, what it is, how it does things. I use several ways. One of them is the well-known model/view/controller (MVC) architecture.

Most web frameworks explain themselves in that way and every web framework sees MVC a bit differently. For Django, for instance, you also see “model/view/template” as an explanation. I myself think that MVC offers a pretty helpful way to look at Django. Only, it is pretty hard to explain right. And I have to leave out quite some details: it is an early chapter. So I’m leaving out middleware, forms, template tags and so on :-)

Here’s the core of my explanation. If you’ve got any suggestion/gripe/complaint/praise, please share it as a comment on this article. That way I can improve my chapter! I’m well aware that you can explain Django in lots of ways so I want to make sure I don’t foul it up.

  • Controller. A controller is the heart of the system, it steers everything. For a web framework, this means handling requests and responses, setting up database connections and loading add-ons. For this, Django reads a settings file so that it knows what to load and set up. And Django reads a URL config file that tells it what to do with the incoming requests from browsers.

  • Model. The model layer in Django means the database plus the Python code that directly uses it. It models reality. You capture whatever your website needs in database tables. Django helps you write Python classes (called models) that tie 1:1 to the database tables.

  • View. The view layer is the user interface. Django splits this up in the actual HTML pages and the Python code (called views) that renders them. And it also has an automatic web admin interface for editing the models.

I’ve also made a video where I’m explaining it this way. I’m experimenting to find a good setup for making such a video, so it isn’t perfect yet. Especially the sound can be better, I think.

video explaining Django with the model/view/controller paradigm
 
vanrees.org logo

About me

My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.

Weblog feeds

Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):