After creating a Django app, Django automatically creates several files and folders. Each file has a specific purpose. Understanding the app structure is important because you will use these files while building Django applications.
Suppose we create an app named students:
python manage.py startapp students
Django creates a structure similar to:
students/
├── __init__.py
├── admin.py
├── apps.py
├── migrations/
├── models.py
├── tests.py
└── views.py
The __init__.py file tells Python that the directory can be
treated as a Python package.
students/
└── __init__.py
It is normally created automatically when the Django app is created.
The views.py file contains views. A view receives a request and
returns a response.
from django.http import HttpResponse
def home(request):
return HttpResponse("Welcome to Students")
Views contain the logic that controls what users see when they visit URLs.
The models.py file is used to define database models.
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
A model generally represents data that Django will work with in the database.
The admin.py file is used to configure models for the Django
administration site.
from django.contrib import admin
from .models import Student
admin.site.register(Student)
After registering the model, it can be managed through the Django admin site.
The apps.py file contains the configuration class for the Django
app.
from django.apps import AppConfig
class StudentsConfig(AppConfig):
name = 'students'
Django creates this file automatically when the app is created.
The migrations folder stores migration files used to apply model
changes to the database.
students/
└── migrations/
└── __init__.py
Migration files are generated using Django management commands.
The __init__.py file inside the migrations folder makes the
migration directory a Python package.
migrations/
└── __init__.py
Django creates this file when the app is created.
The tests.py file can contain automated tests for the app.
from django.test import TestCase
class StudentTest(TestCase):
pass
Tests help developers check whether application functionality works correctly.
The app structure is created using the startapp command.
python manage.py startapp students
The command creates the standard Django app files and folders.
A Django project can contain multiple apps.
myproject/
│
├── manage.py
│
├── myproject/
│ ├── settings.py
│ ├── urls.py
│ ├── asgi.py
│ └── wsgi.py
│
├── students/
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
│
└── courses/
The project package contains the main configuration of the Django project. An app contains functionality for a specific part of the website.
myproject/
├── settings.py
├── urls.py
├── asgi.py
└── wsgi.py
students/
├── models.py
├── views.py
└── admin.py
When an app needs to be recognized by Django, add it to
INSTALLED_APPS in settings.py.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'students',
]
Instead of the simple app name, the application configuration class can also be specified.
INSTALLED_APPS = [
...
'students.apps.StudentsConfig',
]
This refers to the configuration class defined in apps.py.
Django does not automatically create an app-level urls.py with
startapp. It can be created manually.
students/
├── urls.py
├── views.py
├── models.py
└── admin.py
This allows URL patterns to be organized inside the app.
Create students/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home),
]
The empty path represents the base URL of the app when its URLs are included under a prefix.
Use include() in the project's main urls.py.
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('students/', include('students.urls')),
]
The URL pattern points to a view function.
path('', views.home)
The view can return a response:
def home(request):
return HttpResponse(
"Student Home Page"
)
Together, the URL and view handle the browser request.
When models are created or changed, Django can generate migration files.
python manage.py makemigrations
Then apply them using:
python manage.py migrate
Example in students/models.py:
from django.db import models
class Student(models.Model):
name = models.CharField(
max_length=100
)
mobile = models.CharField(
max_length=15
)
email = models.EmailField()
This model can represent student information in the application.
The model can be registered in admin.py.
from django.contrib import admin
from .models import Student
admin.site.register(Student)
The model can then be managed through the Django admin interface.
An app can also contain templates for displaying HTML pages. A common structure is:
students/
└── templates/
└── students/
└── home.html
The additional students folder helps avoid template name conflicts
between multiple apps.
An app can also contain static files such as CSS, JavaScript, and images.
students/
└── static/
└── students/
├── css/
├── js/
└── images/
These files can be loaded using Django's static file system.
students/
│
├── __init__.py
├── admin.py
├── apps.py
├── models.py
├── tests.py
├── views.py
│
├── migrations/
│ └── __init__.py
│
├── templates/
│ └── students/
│ └── home.html
│
└── static/
└── students/
├── css/
├── js/
└── images/
Not every project needs all optional folders. They can be created as the application grows.
| File / Folder | Purpose |
|---|---|
models.py |
Database models |
views.py |
Request and response logic |
admin.py |
Admin configuration |
apps.py |
App configuration |
migrations/ |
Database migration files |
tests.py |
Application tests |
As an application becomes larger, additional modules and folders can be created to keep the code organized.
For example:
students/
├── models.py
├── views.py
├── forms.py
├── urls.py
├── services.py
├── templates/
└── static/
The exact structure can vary according to the project's requirements.
INSTALLED_APPS when requiredinclude()myproject/
│
├── manage.py
│
├── myproject/
│ ├── settings.py
│ ├── urls.py
│ ├── asgi.py
│ └── wsgi.py
│
└── students/
├── migrations/
├── templates/
├── __init__.py
├── admin.py
├── apps.py
├── models.py
├── tests.py
└── views.py
This structure provides a clear separation between project configuration and application functionality.
Step 1: Create the app
python manage.py startapp students
Step 2: Add the app
INSTALLED_APPS = [
...
'students',
]
Step 3: Create a view
from django.http import HttpResponse
def home(request):
return HttpResponse(
"Welcome to Students"
)
Step 4: Create app URLs
from django.urls import path
from . import views
urlpatterns = [
path('', views.home),
]
Step 5: Include app URLs
path(
'students/',
include('students.urls')
)
Now the students app is connected to the project.
Understanding the Django app structure makes it easier to build and maintain Django projects.
__init__.py makes the directory a Python package.views.py contains views.models.py contains database models.admin.py configures the admin interface.apps.py contains app configuration.tests.py can contain automated tests.migrations/ contains migration files.templates/ can contain HTML templates.static/ can contain CSS, JavaScript, and images.views.py handles request and response logic.models.py defines database models.admin.py configures models for the admin site.apps.py contains application configuration.migrations/ stores database migration files.urls.py.Question: Which file is mainly used to define database models in a Django app?