A Django project is usually divided into smaller components called applications. A Django app is designed to handle a particular part of a website, such as students, products, blogs, accounts, or courses.
A Django app is a Python package that contains code for a specific feature or area of a Django project.
Examples include:
A project is the complete Django website configuration, while an app usually handles a specific feature or functionality.
Project
├── App 1
├── App 2
└── App 3
Django apps help organize a large project into manageable parts.
The startapp management command is used to create a new Django app.
python manage.py startapp students
This command creates a new app named students.
Choose a meaningful name that describes the purpose of the app.
python manage.py startapp students
python manage.py startapp courses
python manage.py startapp blog
python manage.py startapp accounts
Meaningful names make projects easier to understand.
Run the startapp command from the directory containing
manage.py.
myproject/
├── manage.py
└── myproject/
Then run:
python manage.py startapp students
After running startapp, Django creates a folder for the app.
students/
├── __init__.py
├── admin.py
├── apps.py
├── migrations/
├── models.py
├── tests.py
└── views.py
The __init__.py file marks the directory as a Python package and
allows Python to treat the app as a package.
students/
└── __init__.py
It is normally created automatically by Django.
The views.py file contains views that handle requests and return
responses.
from django.http import HttpResponse
def home(request):
return HttpResponse("Student App")
Views contain application logic related to the app.
The models.py file is used to define database models for the app.
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
Models are used with Django's ORM to work with database records.
The admin.py file is used to configure models for the Django admin site.
from django.contrib import admin
from .models import Student
admin.site.register(Student)
The apps.py file contains configuration for the Django application.
from django.apps import AppConfig
class StudentsConfig(AppConfig):
name = 'students'
Django creates this configuration automatically when the app is created.
The migrations folder contains database migration files created by
Django.
students/
└── migrations/
└── __init__.py
Migration files record changes to database structure.
The tests.py file can contain automated tests for the application.
from django.test import TestCase
class StudentTest(TestCase):
pass
Testing helps verify that application functionality works as expected.
After creating an app, it should generally be added to
INSTALLED_APPS when Django needs to discover its models, migrations,
templates, or other app components.
INSTALLED_APPS = [
...
'students',
]
Open the project's settings.py file and add the app to
INSTALLED_APPS.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'students',
]
The application configuration class can also be referenced in
INSTALLED_APPS.
INSTALLED_APPS = [
...
'students.apps.StudentsConfig',
]
This makes the app's configuration explicit.
Open students/views.py:
from django.http import HttpResponse
def home(request):
return HttpResponse(
"Welcome to Student App"
)
This creates a simple view for the students app.
Django does not automatically create an app-level urls.py when
using startapp. We can create it manually.
students/
├── views.py
├── urls.py
└── models.py
The app's URL patterns can then be kept separate from the project's URLs.
from django.urls import path
from . import views
urlpatterns = [
path('', views.home),
]
This connects the app's URL to its view.
The project's main urls.py can include the app's URL patterns.
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('students/', include('students.urls')),
]
If the project includes the app using:
path('students/', include('students.urls'))
and the app contains:
path('', views.home)
then visiting the following URL can open the view:
/students/
An app can contain models that represent database tables.
from django.db import models
class Student(models.Model):
name = models.CharField(
max_length=100
)
email = models.EmailField()
After creating or changing models, create migration files using:
python manage.py makemigrations
Django detects model changes and creates migration instructions.
Use the migrate command to apply migration changes to the database.
python manage.py migrate
This creates or updates database structures according to the migrations.
A single Django project can contain multiple apps.
myproject/
├── manage.py
├── myproject/
│
├── students/
├── courses/
├── accounts/
└── blog/
Each app can handle a different part of the website.
Django apps can be designed as reusable components.
For example, a blog app can potentially be reused in another Django project with appropriate configuration and dependencies.
startapp outside the intended project directoryINSTALLED_APPS when requiredStep 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 Student App"
)
Step 4: Create students/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home),
]
Step 5: Include app URLs
from django.urls import path, include
urlpatterns = [
path(
'students/',
include('students.urls')
),
]
Creating apps is an important part of organizing Django projects. Each app can focus on a particular area of the website.
python manage.py startapp appname to create an app.views.py contains views.models.py contains database models.admin.py configures the admin site.apps.py contains app configuration.migrations stores migration files.INSTALLED_APPS when required.urls.py.python manage.py startapp appname to create an app.views.py,
models.py, and admin.py.INSTALLED_APPS when its components need to be
discovered by Django.Question: Which command is used to create a new Django app?