Getting Started with Django: Models, Views, Templates, the ORM and the Admin
A tiny Django menu app with one model, one view, one URL, one template and the admin. What each file does, how one request travels through them, and the real output of a run.
What you will learn
- Explain what batteries included means by reading INSTALLED_APPS
- Name the job of the model, the view and the template, and how MVT maps to MVC
- Follow one request from urls.py through the view, the ORM and the template
- Register a model with the admin and see the admin answer before and after login
Before you start
- Python classes and functions
- Basic SQL tables and rows
- HTTP status codes
Picture two kitchens. In the first one the shelves are empty, so you go out and buy every ingredient before you can cook anything. In the second one the shelves are already full: routing, a database layer, page templates, forms, login, sessions and an admin. You walk in and start cooking, and you only take the jars your dish needs.
The second kitchen is Django. People call it "batteries included", because the common parts of a web app come in the box. In this article we use a tiny menu app with one model, one view, one URL, one template and the admin. Once you know which file does which job, Django is much easier to build with, and much easier to review when an AI tool writes the code for you.
What comes in the box
You can see the jars on the shelf in config/settings.py, in the INSTALLED_APPS list:
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"menu",
]
django.contrib.adminis the admin site, ready to use.authhandles users, passwords and login.sessionsremembers a user from one request to the next, andmessagesshows short notes like "saved".menu, the last line, is our own app. Django's apps and ours sit in the same list.
Django comes with sensible defaults, so you don't repeat the same setup in every project, and you can change any of them. The price is that there's more to learn at the start.
Model, view, template
Django splits an app into three parts. The short name is MVT:
- Model: the data. It lives in
models.pyand describes what you store. - View: the logic. It lives in
views.py. It takes a request, gets the data and decides what to send back. - Template: the page. An HTML file with gaps that Django fills in.
You may know MVC (model, view, controller) from other frameworks. There, the "view" is the page and the controller holds the logic. Django's view does the controller's job, and Django's template does the page's job. Same idea, different names.
How one request travels
The browser asks for /dishes/. The URL router in urls.py finds the matching path and hands the request to a view. The view asks the model for the available dishes. The model turns that into SQL, and the database sends the rows back. The view passes the dishes to the template, the template turns them into HTML, and the HTML goes back to the browser as the response.
The sections below follow that trip from the bottom up, file by file.
The model becomes a table
Here is menu/models.py:
from django.db import models
class Dish(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(
max_digits=6, decimal_places=2
)
available = models.BooleanField(default=True)
def __str__(self):
return self.name
Dish is a normal Python class that inherits from models.Model. Each class becomes one table, and Django names it menu_dish: the app name, then the model name. Each field becomes a column. name is text up to 100 characters, price is a decimal, and available is true or false, true by default.
Django writes the SQL for you. This is the CREATE TABLE statement it made from that class (python manage.py sqlmigrate menu 0001):
CREATE TABLE "menu_dish" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(100) NOT NULL, "price" decimal NOT NULL, "available" bool NOT NULL);
The project uses SQLite, a database stored in one file, db.sqlite3, so there's no database server to install. The migrate command creates the tables. That cycle gets its own article: Django models, migrations and the admin.
The ORM in action
ORM stands for object relational mapper. You work with Python objects, and Django talks to the database for you. The project has a small script, check_pages.py, that uses the model. Its first half:
Dish.objects.create(name="Pasta", price="9.50")
Dish.objects.create(name="Soup", price="4.00")
Dish.objects.create(
name="Cake", price="5.25", available=False
)
print("Dishes saved:", Dish.objects.count())
print(Dish.objects.filter(available=True))
print(Dish.objects.get(name="Soup").price)
Dishes saved: 3
<QuerySet [<Dish: Pasta>, <Dish: Soup>]>
4.00
createsaves a row. Cake is saved as not available.filter(available=True)gives back only the rows that match, so Cake stays out.getgives back exactly one row, so we can read Soup's price.
There's no SQL in that code. Because Django builds the queries, values like a dish name are passed in safely, which protects you from SQL injection.
One setting picks the database
Django learns which database to use from DATABASES in settings.py:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
If a project later moves to PostgreSQL, you change these settings and install its driver. Your models and queries mostly stay the same, but the new database is empty, so you still run the migrations on it.
The URL and the view
menu/urls.py says: when someone asks for dishes/, call the dish_list view.
from django.urls import path
from . import views
urlpatterns = [
path("dishes/", views.dish_list),
]
The project's own config/urls.py includes this file, and adds admin/ for the admin site:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("menu.urls")),
]
A view is just a function, and Django passes it the request. Here is menu/views.py:
from django.shortcuts import render
from .models import Dish
def dish_list(request):
dishes = Dish.objects.filter(available=True)
return render(
request,
"menu/dish_list.html",
{"dishes": dishes.order_by("name")},
)
It asks the model for the available dishes, ordered by name. Then render takes the request, the template's name and a dictionary. That dictionary is the context, and the key dishes is the name the template will use.
The template fills in the gaps
menu/templates/menu/dish_list.html is mostly plain HTML:
<h1>Today's menu</h1>
<ul>
{% for dish in dishes %}
<li>{{ dish.name }}: {{ dish.price }}</li>
{% empty %}
<li>Nothing today</li>
{% endfor %}
</ul>
{% ... %}is a tag. It does something; here it's aforloop overdishes.{{ ... }}prints a value, sodish.nameanddish.pricego straight into the page.{% empty %}gives a fallback for when the list has no dishes.
Django also escapes the values it prints, so text like a script tag shows up as text and doesn't run.
Running the request without a server
The second half of check_pages.py uses Django's test client, which acts like a browser inside Python:
client = Client()
resp = client.get("/dishes/")
print("GET /dishes/ ->", resp.status_code)
print(resp.content.decode())
GET /dishes/ -> 200
<h1>Today's menu</h1>
<ul>
<li>Pasta: 9.50</li>
<li>Soup: 4.00</li>
</ul>
Status 200 means OK. Pasta and Soup are on the page, and Cake isn't, because the view filtered it out. The model, the view and the template each did one job.
The admin, for free
One more jar from the shelf. menu/admin.py has one real line:
from django.contrib import admin
from .models import Dish
admin.site.register(Dish)
That line puts Dish in the admin site. The script then asks for /admin/ without signing in, creates a test superuser, logs in and opens the dish list in the admin:
GET /admin/ -> 302 /admin/login/?next=/admin/
Signed in -> 200
Admin lists Dish: True
Without a login, the answer is 302, a redirect to the admin login page. Signed in, the dish page returns 200 and lists Pasta. You get pages to list, add, edit and delete dishes without writing them, which is handy for you and your team when you need to look at or fix data.
One project, many apps
config is the project package. It holds the settings and the main URLs for the whole site. menu is an app: one folder for one feature, with its own models, views, URLs, admin and templates. A bigger site has several apps inside one project, sharing the same settings and database, and everything runs as one application, a monolith.
This is also why the structure matters when you work with AI. When you ask an agent to add a feature, you can tell it which app the change belongs in, and then check that each file is doing its own job. The next step is to build a project and app yourself: setting up your first Django project and app.
Common mistakes
- Expecting Django's "view" to be the page. In Django the view is the logic. The page is the template.
- Forgetting the app in
INSTALLED_APPS. Django won't find its models or templates. - Mixing up the template syntax.
{% %}runs a tag,{{ }}prints a value. - Thinking the ORM removes the database. Switching databases is a settings change, but you still run the migrations on the new one.
Quick recap
| Piece | File | Job in the menu app |
|---|---|---|
| Model | menu/models.py | Dish becomes the menu_dish table |
| URL router | menu/urls.py, config/urls.py | dishes/ goes to dish_list |
| View | menu/views.py | gets available dishes, calls render |
| Template | dish_list.html | loops over dishes, prints name and price |
| ORM | Dish.objects | create, filter, get without SQL |
| Admin | menu/admin.py | one register line, full admin pages |
Try it yourself
Mark Soup as not available in check_pages.py, run it again, and predict which dishes the page shows before you look at the output.
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
Django, FastAPI or Flask? The Same Blog Feature in Three Shapes, and How to Choose
One small project lists two blog posts three times: as a Django view, a Flask route and a FastAPI endpoint. See each shape with its real output, what each framework includes, and a three-question way to choose.
Your First Django Project and App: uv, startproject, startapp and a First View
Set up a Django project called blogproject with uv, add an app called blog, write one view that returns Hello blog, give it the empty path and run the server. Every command with its real output.
The Django Project Folder Structure, File by File: Reading a Blog an AI Agent Built
A tour of a finished Django blog: manage.py, the project package, the blog app with its models, views, templates, forms and admin, a custom command, the REST API and Swagger docs, plus the review points check --deploy found.
How a Request and a Login Flow Through Django: URLconf, ORM, CSRF, Password Hashes and Sessions
Follow the home page from URLconf to SQLite and back (8 queries), then a login: the csrftoken, a 403 without it, the pbkdf2 password hash, the django_session row, the sessionid cookie, and a review finding about who may write posts.
