Lesson 23 of 60 – Django App Structure
38%

Django App Structure

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.

Note: A Django app usually contains files for models, views, administration, application configuration, tests, and database migrations.

1. Basic Django App Structure

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

2. __init__.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.

3. views.py

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.

4. models.py

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.

5. admin.py

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.

6. apps.py

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.

7. migrations Folder

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.

8. migrations/__init__.py

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.

9. tests.py

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.

10. Creating an App

The app structure is created using the startapp command.

python manage.py startapp students

The command creates the standard Django app files and folders.

11. App and Project Structure

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/

12. App vs Project Package

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

13. Adding an App to INSTALLED_APPS

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',
]

14. Using AppConfig

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.

15. Creating App URLs

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.

16. Basic App urls.py

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.

17. Connecting App URLs to Project URLs

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')),
]

18. URL and View Relationship

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.

19. Models and Migrations

When models are created or changed, Django can generate migration files.

python manage.py makemigrations

Then apply them using:

python manage.py migrate

20. Model Example

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.

21. Register Model in Admin

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.

22. Templates Inside an App

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.

23. Static Files Inside an App

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.

24. Complete App Structure

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.

25. Where Application Logic Goes

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

26. Organizing a Large App

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.

27. Common App Structure Mistakes

  • Putting app files inside the wrong project folder
  • Forgetting to add the app to INSTALLED_APPS when required
  • Creating URL patterns but forgetting include()
  • Creating models but forgetting migrations
  • Using incorrect import paths
  • Keeping templates in an unexpected directory
  • Using incorrect static file paths

28. Recommended Basic Structure

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.

29. Complete App Workflow

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.

30. Summary of Django App Structure

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.
  • A project can contain multiple apps.

📌 Key Points

  • A Django app contains functionality for a specific part of a project.
  • 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.
  • An app can have its own urls.py.
  • Apps can contain templates and static files.
  • A Django project can contain multiple apps.

🧠 Quick Quiz

Question: Which file is mainly used to define database models in a Django app?