So like this is all I learnt today about MVC
MVC stands for:
- Model – Handles data and business logic.
- View – Displays data to the user.
- Controller – Accepts user input and processes it.
Hence, each has it’s own responsibility in keeping our code clean and organized.
Django's Twist:
From what I’ve learned Django uses the MTV architecture, which maps like this:
Django MTV Traditional MVC Purpose
Model Model Handles the data, database, and business logic
Template View Displays the data to the user (HTML, CSS)
View Controller Handles the logic and connects everything
Yup, Django’s “View” is actually the Controller in the traditional sense, and the “Template” is the actual View.
Crazy I know.
This matters cause…
Understanding this mapping clears up confusion when navigating Django projects. To ease more understanding, this is what each component looks like in action:
Model (models.py
): Defines your database structure.
View (views.py
): Contains the logic to fetch data and choose what template to render.
Template (.html files
): Renders the UI using context passed from the view.
Building a Basic Django App Example
Here’s a quick overview of how these parts fit together in a blog app:
models.py
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
views.py
def post_list(request):
posts = Post.objects.all()
return render(request, 'blog/post_list.html', {'posts': posts})
<!-- post_list.html -->
<h2>{{ post.title }}</h2>
<p>{{ post.content }}</p>
My last take on MVC is that…
While Django uses different naming conventions, it follows the same principles of MVC.
Anyways, I’m Learning to embrace the MTV structure since it is key to mastering Django and building clean and efficient apps.
Alright that’s it for today, peace
Top comments (0)