Django Fundamentals: MVT, Models, the ORM, Admin and Migrations
Django fundamentals are the core ideas every Django project uses. Django follows a model, view, template (MVT) pattern: models define your data, views hold the logic for each request, and templates render HTML. URL patterns map web addresses to views.
Topic: Python for AI & Automation in AI & Data · Course information
Quick answer: Django fundamentals are the core ideas every Django project uses. Django follows a model, view, template (MVT) pattern: models define your data, views hold the logic for each request, and templates render HTML. URL patterns map web addresses to views. The ORM lets you query the database with Python instead of raw SQL, migrations keep the database schema in step with your models, and the built-in admin gives you a ready-made interface to manage data. Learn these in that order with the official tutorial, using a supported Django release such as 5.2 LTS or 6.1.
Key takeaways
- Django calls its pattern MVT: model, view, template.
- A project holds settings; apps hold features.
- The ORM turns Python queries into parameterised SQL.
- Migrations record every change to your models.
- The admin site is generated from your models with little code.
Table of Contents
- What Is the MVT Pattern in Django?
- What Is the Difference Between a Project and an App?
- How Do URLs, Views and Templates Work Together?
- What Are Models and the ORM?
- How Do Migrations Work?
- What Does the Django Admin Give You?
- In What Order Should You Learn the Fundamentals?
- References
What Is the MVT Pattern in Django?
The Django FAQ explains that what other frameworks call the controller is handled by the framework itself, so Django uses the terms model, view and template.
| Part | Role | Lives in |
|---|---|---|
| Model | Defines data and database tables | models.py |
| View | Receives a request, returns a response | views.py |
| Template | Presents data as HTML | templates folder |
| URLconf | Maps URLs to views | urls.py |
What Is the Difference Between a Project and an App?
A project is the whole site: settings, root URLs and configuration. An app is a self-contained feature, such as a blog or polls, that can be reused. The official tutorial starts with these commands inside a virtual environment:
python -m pip install Django
django-admin startproject mysite
cd mysite
python manage.py startapp polls
python manage.py runserver
The runserver command is for local development only, never production.
How Do URLs, Views and Templates Work Together?
When a request arrives, Django checks the URL patterns in order, calls the matching view and passes it an HttpRequest. The view fetches data, often from models, and returns an HttpResponse, usually by rendering a template. Templates use variables, tags and filters, and they autoescape HTML by default, which protects against many cross-site scripting bugs.
What Are Models and the ORM?
A model is a Python class whose attributes become database columns. Field types such as CharField, IntegerField, DateTimeField and ForeignKey define the schema and relationships.
The ORM lets you make queries such as filtering, ordering and following relationships, all in Python. Querysets are lazy: nothing hits the database until you use the results. Because the ORM parameterises values, it helps prevent SQL injection, the main defence the OWASP SQL Injection Prevention Cheat Sheet recommends. Take care if you ever use raw SQL.
How Do Migrations Work?
Migrations are Django's way of applying model changes to the database schema. You change a model, generate a migration file, review it and apply it:
python manage.py makemigrations polls
python manage.py showmigrations
python manage.py migrate
| Command | What it does |
|---|---|
| makemigrations | Creates migration files from model changes |
| showmigrations | Lists migrations and whether they are applied |
| migrate | Applies pending migrations to the database |
| sqlmigrate | Shows the SQL a migration will run |
Commit migration files to Git so every environment builds the same schema.
What Does the Django Admin Give You?
The Django admin reads your models and builds pages to create, edit and delete records. Register a model in admin.py, create a superuser with python manage.py createsuperuser, and log in. It is designed for trusted staff, not as a public front end. In production, protect it with strong passwords and consider a non-default URL.
In What Order Should You Learn the Fundamentals?
- Python basics and virtual environments.
- Projects, apps and settings.
- URLs, views and templates.
- Models, the ORM and migrations.
- The admin site.
- Forms and authentication.
- Tests with the Django test runner.
Check the supported versions before you start; Django 5.2 is the LTS release with extended support until April 2028. Compare these ideas with Flask fundamentals to see what Django builds in. If you want an intensive format, read our Django bootcamp guide, or contact us about Django fundamentals training.
References
Frequently Asked Questions
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Wow
0
Sad
0
Angry
0