Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on. You've got this! π
What is Django & MVT Architecture
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It follows the Model-View-Template (MVT) architecture - Django's version of MVC. Understanding MVT is essential before building any Django project.
What is Django?
Django is a free, open-source web framework written in Python. Created in 2003 by Adrian Holovaty and Simon Willison at the Lawrence Journal-World newspaper, Django was designed to build web applications rapidly with less code. It follows the 'batteries-included' philosophy - providing everything you need out of the box: ORM, admin panel, authentication, URL routing, and more.
QuerySets are LAZY - no DB hit until evaluated.
Why Django?
- Batteries-included - ORM, admin, auth, forms, all built-in
- Rapid development - build production apps in days, not months
- Security - protects against SQL injection, XSS, CSRF by default
- Scalable - powers Instagram, Pinterest, Disqus, Mozilla
- Excellent documentation - one of the best-documented frameworks
- Large community - thousands of packages on PyPI
- DRY principle - Don't Repeat Yourself is a core philosophy
- Python-based - leverage Python's entire ecosystem
MVT Architecture
# Model-View-Template (MVT) Architecture
#
# Model β Defines data structure (database tables)
# View β Handles business logic (processes requests)
# Template β Handles presentation (HTML output)
#
# Request Flow:
# 1. User sends HTTP request
# 2. Django URL router matches the URL to a View
# 3. View processes logic, queries Model if needed
# 4. View passes data to Template
# 5. Template renders HTML response
# 6. Response sent back to user
#
# Django MVT vs MVC:
# MVT Model = MVC Model
# MVT View = MVC Controller
# MVT Template = MVC ViewTip
Tip
Django follows the 'batteries included' philosophy. Most web features (auth, admin, ORM) are built-in - don't reinvent them.
Common Mistake
Warning
Confusing Django with Flask. Django is a full-stack framework with conventions; Flask is micro and gives you full control. Choose based on project size.
Practice Task
Note
(1) Install Django: pip install django. (2) Run django-admin startproject mysite. (3) Run python manage.py runserver and visit localhost:8000.
Quick Quiz
Key Takeaways
- Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design.
- Batteries-included - ORM, admin, auth, forms, all built-in
- Rapid development - build production apps in days, not months
- Security - protects against SQL injection, XSS, CSRF by default