Apps
A Django project is the whole website: its settings, its main URL list, and how it's deployed. An app is one self-contained part of it, such as courses, accounts or a blog, with its own models, views, templates and URLs.
Creating an app
From the folder containing manage.py:
python manage.py startapp coursesDjango creates a folder of starting files:
courses/
__init__.py makes the folder a Python package
admin.py registers models with the admin site
apps.py the app's configuration
migrations/ database changes, generated for you
__init__.py
models.py the app's data: database tables as Python classes
tests.py automated tests
views.py functions that handle requestsYou'll usually add two files yourself: urls.py for the app's URL patterns, and a templates/courses/ folder for its HTML.
Registering the app
A new app does nothing until the project knows about it. Add it to INSTALLED_APPS in the project's settings.py:
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"courses", # your app
]The django.contrib entries are apps too, the ones Django ships with: the admin site, user accounts and logins, sessions, and static file handling. A project is a collection of apps, some yours and some Django's.
Once it's registered, Django finds the app's models (for migrations), its templates (in courses/templates/), its admin registrations and its static files.
apps.py
from django.apps import AppConfig
class CoursesConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "courses"
verbose_name = "Course catalogue" # the name shown in the adminThe generated class is enough to start with. verbose_name changes how the app is labelled in the admin.
A project with several apps
codemirai/ the project folder
manage.py
config/ project settings and the main urls.py
settings.py
urls.py
wsgi.py
courses/ catalogue: courses and lessons
accounts/ sign-up, login, profiles
progress/ XP, badges, completed lessonsWhat makes a good app
- One clear job. If you can't describe an app in a short sentence, it's probably two apps.
- Few dependencies.
progressmay usecourses, butcoursesshouldn't need to know aboutprogress. - Reusable where possible. A well-designed app could move to another project. Many popular Django packages, like
django-allauthfor accounts, are just apps you install and add toINSTALLED_APPS.
Don't worry about getting the split perfect at the start. Many good projects begin with one app and divide it when it grows.