Model-View-Controller (MVC) in Django

Model-View-Controller (MVC) in Django

ยท

2 min read

Model-View-Controller is a software architectural pattern used to implement a logic between user's interface, data and the controlling flow. It is divided into three parts:

  1. Model: contains the data to be sent or updated.

  2. View: handles layout and display.

  3. Control: routes commands to the model and view parts.

Example

Imagine a grocery shopping site, all we need is the name, price and lists of the grocery on the shelf. Here's how to use an MVC below;

The Model

The model tells what data the app or website should contain. If the data changes, then the model will usually update the view (so the display can change as needed) and sometimes the controller, if different logic is needed to control the updated view.

Going back to the grocery shopping list app, the model would specify what data the list items should contain โ€” item, price, etc. โ€” and what list items are already present.

The view

The view defines how the app'e data should be displayed.

In the grocery shopping list app, the view would define how the list is presented to the user, and receive the data to display from the model.

The controller

The controller contains logic that updates the model and/or view in response to input from the users of the app.

So for example, the grocery shopping list could have input forms and buttons that allow us to add or delete items. These actions require the model to be updated, so the input is sent to the controller, which then manipulates the model as appropriate, which then sends updated data to the view.

You might however also want to just update the view to display the data in a different format, e.g., change the item order to alphabetical, or lowest to highest price. In this case the controller could handle this directly without needing to update the model.

So, with the above illustration, I believe you've gotten the basic idea of how Model-View-Controller works in Django and generally, all web frameworks. To implement it fully in your project, please, contact the documentation of the the language you'll be using. See you again ๐Ÿ‘‹๐Ÿพ

ย