Django’s Architectural Pattern - The Turning Gear

Django’s Architectural Pattern

Michael Hudelson 10.23.2018

Django’s Architectural Pattern
The Model-View-Controller (MVC) design pattern is far and away the most widely accepted design pattern used to develop modern web applications. In fact, it is used by nearly every major web framework in production today. More importantly, it is the architectural pattern implemented in Django under the guise of Model-View-Template (MVT) – but more on that later. Anyway, despite the fact that this pattern is so commonly used, there is still a ton of confusion and misinformation around how it should and should not be implemented. Thankfully for you, I am here to clear things up in relatively straightforward way.

The Model-View-Controller (MVC) Architectural Pattern
To kick things off, let’s discuss the generic Model-View-Controller pattern.
– Model (M) – The model is meant to represent the data in the application. This is typically done through some sort of (typically) class-based abstraction layer over a database.
– View (V) – The view is meant to handle generating what the user will see. In the web application world, this usually means generating HTML.
– Controller (C) – The controller is meant to handle the flow of the application. This usually means handling a user’s interactions with your web application by deciding which single view and set of models to use to satisfy the request.
The above are generic explanations of the architectural pattern, not a set of hard and fast rules. They only really give a general sense of how the pattern should be implemented, which is why it is implemented so differently by each framework and individual. While it can be a bit troublesome and confusing to someone who is new to development, it is also the reason generic design patterns are so valuable. They offer the flexibility to fit the situation they find themselves in.

The Model-View-Template (MVT) Architectural Pattern
You may be asking yourself how Model-View-Template (MVT) and Model-View-Controller (MVC) relate in the world of Django. It’s a bit confusing at first, but it is actually very straightforward.
– The Model in MVC is the same as the Model in MVT.
– The View in MVC is the same as the Template in MVT.
– The Controller in MVC is the same as the View in MVT.
I encourage you to read the bullet points above one more time, because it is not a typo. It is just an unfortunate naming decision that was made by Django, and now we are stuck with it. Inconsistent naming aside, Django’s MVT is in fact the MVC pattern by another name.

Data Flow Through Django
To give you some context, I want to quickly highlight how the data flows through Django at a high level.
1.) A user makes a request for one of your Django web pages.
2.) A request is received by the web server.
3.) That request is handed off to Django.
4.) Django uses your defined URL paths for picking which view to use.
5.) Your view decides which models are needed and passes them off to the template.
6.) Your template is used to generate the pages HTML.
7.) The generated HTML is passed back to the user’s browser.
This flow can be visualized in the diagram below. It is also worth noting that this is a high level overview, so some details were glazed over as they are really not important to understanding how to use Django’s MVT pattern.

Conclusion
The use of design patterns help standardize our projects, and the MVC (or MVT) pattern is no exception. By understand the basics of this pattern, you can hop from one project to the next with relative ease. Each project may make different usage of the pattern, but typically the core values are the same.

IF YOU CAN DREAM IT, WE CAN BUILD IT.

LET US GET STARTED ON YOUR PROJECT TODAY!