Django Apps
In the previous lesson, we created a Django project. Now it is time to understand what a Django App is and why we need it. A project alone is just an empty building — apps are the actual rooms where the real work happens.
What is a Django App?
A Django App is a small, self-contained module inside your project that handles one specific feature or section of your website. A single Django project can have many apps working together.
Example: Imagine a shopping mall (project). Inside it, there are separate shops — a food court, a clothing store, and a cinema. Each shop is independent but they all exist inside the same mall. Similarly, a Django project can have a blog app, a store app, and a users app — all working inside one project.
Project vs App — What is the Difference?
Many beginners confuse a project with an app. Here is a simple way to remember the difference:
- Project: The overall website or web application. It holds all the settings, database configuration, and the list of all apps.
- App: A specific feature or section inside that project. For example, a blog, a contact form, or a user registration system.
Example: Think of a school (project). It has a library department, a sports department, and an exam department. Each department (app) does its own job, but they all belong to the same school (project).
Creating a New Django App
Make sure you are inside your project folder (where manage.py is located) before running this command. We will create an app called school:
python manage.py startapp school
This creates a new folder named school inside your project with several files inside it automatically.
Understanding the App Structure
After creating the app, your project folder will look like this:
myschool/ <-- Project root folder
│
├── manage.py
├── myschool/ <-- Project settings folder
│ ├── settings.py
│ ├── urls.py
│ └── ...
│
└── school/ <-- Your new app folder
│
├── __init__.py <-- Makes this folder a Python package
├── admin.py <-- Register models to show in admin panel
├── apps.py <-- App configuration file
├── models.py <-- Define your database tables here
├── tests.py <-- Write tests for your app here
├── views.py <-- Write your logic / functions here
└── migrations/ <-- Folder for database migration files
└── __init__.py
admin.py
This file is used to register your models so they appear in Django's built-in Admin Panel. Once registered, you can manage your data from a browser without writing extra code.
- Example: If you create a
Studentmodel, registering it inadmin.pylets the school admin log in and add or edit student records from a simple web interface.
apps.py
This file holds the configuration of your app. It tells Django the name of the app. You rarely need to edit this file manually.
- Example: It is like the nameplate on a department's door — it just says "this room belongs to the school department".
models.py
This is where you define your database tables using Python classes. Each class becomes a table in the database, and each attribute becomes a column.
- Example: A
Studentclass with fields likename,age, andgradewill create a Students table in your database automatically.
views.py
This is the brain of your app. Every function you write here handles a request from the user and sends back a response (usually an HTML page).
- Example: When a student visits the "View Results" page, a view function fetches that student's marks from the database and sends the result page back to their browser.
tests.py
This file is for writing automated tests to check if your app is working correctly. You write test cases here and Django runs them for you.
- Example: Before releasing an exam result page, you write a test that checks "Does the page load correctly?" so you catch bugs before real students see them.
migrations/ folder
This folder stores migration files — instructions that tell Django how to create or update database tables based on your models. Django generates these files automatically.
- Example: If you add a new column
phone_numberto your Student model, Django creates a migration file that says "add a new column to the Students table".
Registering the App in settings.py
Creating an app is not enough — you must tell your project that this app exists. Open myschool/settings.py and find the INSTALLED_APPS list. Add your app name at the bottom:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'school', # <-- Add your app name here
]
If you skip this step, Django will not recognize your app and your models, views, and templates will not work properly.
Example: Imagine you hired a new employee (app) but forgot to add them to the company directory (INSTALLED_APPS). They exist but the company does not officially know about them — so they cannot do any work!
Can a Project Have Multiple Apps?
Yes! In fact, that is the recommended way to build Django projects. Each feature of your website should ideally be its own app so the code stays clean and easy to manage.
python manage.py startapp blog
python manage.py startapp store
python manage.py startapp accounts
Then register all three in INSTALLED_APPS in settings.py.
Example: A newspaper website has separate teams for Sports, Politics, and Entertainment. Each team (app) works independently but together they make one complete newspaper (project).
Quick Recap
# Create a new app
python manage.py startapp school
# Then add 'school' to INSTALLED_APPS in settings.py
