DEV Community

Cover image for Understanding and implementing the MVT/MVC Architecture in Django.
Elizabeth Ng'ang'a
Elizabeth Ng'ang'a

Posted on

Understanding and implementing the MVT/MVC Architecture in Django.

Introduction.

Modern web applications rely on structured design patterns to stay organized and efficient. One of the most popular patterns is the Model-View-Controller (MVC) architecture, which separates an application into three key components: data (Model), logic (Controller), and user interface (View). Django, a powerful Python web framework, follows a similar pattern called Model-View-Template (MVT). While slightly different in naming, MVT serves the same purpose helping developers manage complex applications by keeping code clean, scalable, and easy to maintain.

(MVC)Model-View-Controller

It is a software design pattern used to separate an application into three main parts. This structure makes web development more organized and easier to manage, especially in large projects.
short description of the 3

  1. Model it basically stores and manages data and also connects directly to the database.
  2. View it mainly handles what the user sees and displays data from the model.
  3. controller mainly shows the user actions and logic and connects the model and the view. That is a short description of each.

(MVT)Model-View-Template

it helps developers manage complex applications by keeping code clean.
short description of the 3

  1. Model Handles the data and database structure.
  2. View Contains the logic fetches data from the model and passes it to the template.
  3. Template Handles the presentation ,displays data to the user using HTML.

Here is an Example from my Waste Sorter project

Models
This is where i defined the data for my two apps in the project
Example of the data i added for the 1st app recycle_tips;

Models-recycle tips
Example of the data i added for the 2nd app waste-logs

Models-waste_logs
Making migrations
makemigrations
After creating the models,this command tells Django to look at your models and check if anything changed (like added fields, new models, or edits).It then creates a file called a migration file which is basically a set of instructions telling Django how to update the database structure.

Here is the code used

python manage.py makemigrations
Enter fullscreen mode Exit fullscreen mode

migrate
This command takes the instructions from the migration file created and actually applies them to your database like creating tables or changing columns.

Here is the code

python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

Editing the view.py ,forms and the Template folder.

After i added the changes to my model and migrated them i had to make my view ,form and templates match with the data added in the models.
views look after editing my models

views
Forms
I had to create a file by the name"form.py".Its importance is to help import the form classes (like WasteLogForm) inside this file, and then import them in your views as shown in your code.
Example

form
Templates
I had to update my existing html file under the Templates folder so that they can meet my models requirement.
Example

Template

After making this changes on the views ,forms and the Template there is no need for you to make migrations.You can know test you project using;

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

Here is my output

output1

output2

How Models, Views, Templates, and URLs Work Together

Here is a short description of how i understood.

we as the user makes request → a URL directs it → View processes → Model fetches data → Template displays it.
you can also follow this and listen for more understanding[(https://www.youtube.com/watch?v=cyP4Uw2b2XM)]
Final thoughts
Understanding the MVC and MVT architecture is key to building clean and well-organized web applications. While MVC is a general pattern used in many frameworks, Django uses MVT, which works in a similar way just with different naming.
In Django:

  • The Model handles your data,
  • The View processes the logic,
  • And the Template displays the result to the user. We also explored how Django’s commands like makemigrations and migrate help us apply changes to the database, and how forms, views, and templates bring everything to life on the web page.

Top comments (0)