› Forums › Web Development › HarvardX: CS50W – CS50’s Web Programming with Python and JavaScript › CS50W – Lecture 3 – Django › Understanding Django Apps: models.py, views.py, templates, admin.py, and migrations
- This topic is empty.
-
AuthorPosts
-
June 24, 2026 at 3:43 am #7019
In the previous lesson, we learned that a Django project can contain one or more apps.
For example, in a Django project:
wiki/
│
├── manage.py
│
├── wiki/
│ ├── settings.py
│ └── urls.py
│
└── encyclopedia/Here:
- wiki is the project.
- encyclopedia is an app.
But what exactly is inside an app?
Let’s explore the most important files every Django developer works with.
Creating a New App
To create a Django app:
python manage.py startapp encyclopedia
Django automatically generates:
encyclopedia/
│
├── admin.py
├── apps.py
├── models.py
├── tests.py
├── views.py
├── migrations/
└── init.pyEach file has a specific responsibility.
The Big Picture
Think of a Django app as a restaurant.
Customer
│
▼
Waiter
│
▼
Kitchen
│
▼
Food
│
▼
CustomerIn Django:
User
│
▼
URL
│
▼
View
│
▼
Model
│
▼
DatabaseAnd finally:
Database
│
▼
View
│
▼
Template
│
▼
Browser
1. views.py
Location:
encyclopedia/views.py
Purpose:
Contains the logic that handles requests and returns responses.
Example:
from django.http import HttpResponse
def index(request):
return HttpResponse(“Hello, World!”)When a user visits:
/
Django may execute:
index(request)
and return:
Hello, World!
Why Views Matter
Views are the brains of your application.
They decide:
- What data to retrieve
- What calculations to perform
- Which template to render
- What response to send back
Think of a view as the restaurant waiter who receives the order and coordinates everything else.
2. models.py
Location:
encyclopedia/models.py
Purpose:
Defines how data is stored in the database.
Example:
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()This tells Django:
Create a table called Article
with fields:
title
contentModels Represent Data
Common examples include:
class Student(models.Model):
class Product(models.Model):
class BlogPost(models.Model):
class Employee(models.Model):
Each model eventually becomes a database table.
Database Visualization
Model:
class Student(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()Database table:
ID | Name | Age
1 | John | 20
2 | Alice | 21Django automatically manages the table structure.
3. Templates
Location:
templates/
Example:
encyclopedia/
│
└── templates/
└── encyclopedia/
└── index.htmlPurpose:
Define what users see in their browsers.
Example:
Welcome
{{ username }}
Templates combine:
HTML
+
Data from Viewsto create dynamic pages.
How Templates Receive Data
View:
def index(request):
return render(
request,
“encyclopedia/index.html”,
{
“username”: “Raj”
}
)Template:
Hello {{ username }}
Output:
Hello Raj
Why Templates Are Useful
Without templates:
return HttpResponse(”
Hello Raj
“)
This quickly becomes difficult to manage.
Templates separate:
- Python logic
- HTML design
making applications easier to maintain.
4. admin.py
Location:
encyclopedia/admin.py
Purpose:
Registers models with Django Admin.
Example:
from django.contrib import admin
from .models import Articleadmin.site.register(Article)
After registration:
/admin/
can display and manage Article records.
Why Django Admin Is Powerful
Without Django Admin, you would need to create pages for:
- Adding records
- Editing records
- Deleting records
With Django Admin, Django generates these interfaces automatically.
Example:
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.IntegerField()Register it:
admin.site.register(Product)
Now products can be managed directly through the admin dashboard.
5. Migrations
Location:
encyclopedia/migrations/
Purpose:
Tracks changes made to models and applies them to the database.
Why Migrations Exist
Suppose your model initially looks like:
class Student(models.Model):
name = models.CharField(max_length=100)Later you add:
age = models.IntegerField()
The database must also be updated.
Django uses migrations to keep the model and database synchronized.
Creating Migrations
Command:
python manage.py makemigrations
Example output:
Migrations for ‘encyclopedia’
Django generates migration files such as:
0001_initial.py
Applying Migrations
Command:
python manage.py migrate
Workflow:
models.py
│
▼
makemigrations
│
▼
Migration File
│
▼
migrate
│
▼
Database Updated
6. apps.py
Location:
encyclopedia/apps.py
Example:
from django.apps import AppConfig
class EncyclopediaConfig(AppConfig):
default_auto_field = ‘django.db.models.BigAutoField’
name = ‘encyclopedia’Purpose:
Stores configuration information about the app.
Most beginners rarely modify this file.
7. tests.py
Location:
encyclopedia/tests.py
Purpose:
Contains automated tests.
Example:
from django.test import TestCase
class BasicTest(TestCase):
def test_math(self):
self.assertEqual(2 + 2, 4)Run tests using:
python manage.py test
Django automatically executes all test cases.
Complete Request Flow
Suppose a user visits:
/article/1
The request follows this path:
Browser
│
▼
urls.py
│
▼
views.py
│
▼
models.py
│
▼
Database
│
▼
views.py
│
▼
Template
│
▼
HTML
│
▼
Browser
Real Example
Model:
class Article(models.Model):
title = models.CharField(max_length=100)View:
def article(request):
article = Article.objects.first()
return render(
request,
“article.html”,
{
“article”: article
}
)Template:
{{ article.title }}
Output:
Learning Django
which is displayed in the browser.
How the Pieces Work Together
models.py
│
▼
Database
▲
│
views.py
│
▼
templates
│
▼
BrowserMeanwhile:
admin.py
helps manage data through Django Admin.
And:
migrations/
keeps the database structure synchronized with your models.
Key Takeaways
✅ views.py handles requests and returns responses.
✅ models.py defines database tables.
✅ templates determine what users see.
✅ admin.py enables data management through Django Admin.
✅ migrations synchronize models and databases.
✅ apps.py stores application configuration.
✅ tests.py contains automated tests.
✅ Models, views, and templates form the core of nearly every Django application.
Next Lesson
Understanding Django URLs: path(), URL Parameters, include(), and URL Routing
In the next lesson, we’ll follow a request from the browser into
urls.py, learn how Django matches URLs, capture variables such as article IDs and usernames, and connect URLs to view functions. -
AuthorPosts
- You must be logged in to reply to this topic.
