If youâre curious about web development, then youâve likely encountered the abbreviation MVC, which stands for Model-View-Controller. You may know that itâs a common design pattern thatâs fundamental to many Python web frameworks and even desktop applications.
But what exactly does it mean? If youâve had a hard time wrapping your head around the concept, then keep on reading.
In this tutorial, youâll:
- Approach understanding the MVC pattern through a Lego-based analogy
- Learn what models, views, and controllers are conceptually
- Tie your conceptual understanding back to concrete web development examples
- Investigate Flask code snippets to drive the point home
Maybe you built things with Lego as a kid, or maybe youâre still a Lego-aficionado today. But even if youâve never pieced two Lego blocks together, keep on reading because the analogy might still be a good building block for your understanding.
Get Your Code: Click here to download an example Flask app that will help you understand MVC in Python web apps.
Take the Quiz: Test your knowledge with our interactive âModel-View-Controller (MVC) in Python Web Apps: Explained With Legoâ quiz. Upon completion you will receive a score so you can track your learning progress over time:
Explaining the Model-View-Controller Pattern With Lego
Imagine that youâre ten years old and sitting on your family room floor. In front of you is a big bucket of Lego, or similar modular building blocks. There are blocks of all different shapes and sizes:
- ð¦ð¦ð¦ Some are blue, tall, and long.
- ð¥ Some are red and cube-shaped.
- ð¨ð¨ Some are yellow, big, and wide.
With all of these different Lego pieces, thereâs no telling what you could build!
Just as your mind is filling with the endless possibilities, you hear something coming from the direction of the couch. Itâs your older brother, voicing a specific request. Heâs saying, âHey! Build me a spaceship!â
âAlright,â you think, âthat could actually be pretty cool.â A spaceship it is!
So you get to work. You start pulling out the Lego blocks that you think youâre going to need. Some big, some small. Different colors for the outside of the spaceship, different colors for the engines.
Now that you have all of your building blocks in place, itâs time to assemble the spaceship. And after a few hours of hard work, you now have in front of youâa spaceship:
ð¦
ð¦ð¥ð¦
ð¦ð¥ð¥ð¥ð¦
ð¦ð¥ð¥ð¥ð¥ð¥ð¦
ð¦ð¥ð¥ð¥ð¥ð¥ð¦
ð¦ð¥ð©ð©ð©ð¥ð¦
ð¦ð¥ð©ð¦ð©ð¥ð¦
ð¦ð¥ð©ð©ð©ð¥ð¦
ð¦ð¥ð¥ð¥ð¥ð¥ð¦
ð¦ð¥ð¥ð¥ð¥ð¥ð¦
ð¦ð¥ð¥ð¥ð¥ð¥ð¦
ð¦ð¥ð¥ð¥ð¥ð¥ð¥ð¥ð¥ð¥ð¦
ð¦ð¥ð¥ð¥ð¥ð¥ð¥ð¥ð¥ð¥ð¦
ð¦ð¥ð¨ð¨ð¥ð¥ð¥ð¨ð¨ð¥ð¦
ð¨ð¨ ð¨ð¨
You run to find your brother and show him the finished product. âWow, nice work!â, he says. Then he quietly adds:
Huh, I just asked for that a few hours ago, I didnât have to do a thing, and here it is. I wish everything was that easy.
â Your Brother
What if I told you that building a web application using the MVC pattern is exactly like building something with Lego blocks?
User Sends a Request
Read the full article at https://realpython.com/lego-model-view-controller-python/ »
[ Improve Your Python With ð Python Tricks ð â Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]
Planet Python