CodeMIRAI
0 XP

Apps

2 min read Quiz included

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:

Terminal
python manage.py startapp courses

Django creates a folder of starting files:

Output
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 requests

You'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:

Python
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

Python
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 admin

The generated class is enough to start with. verbose_name changes how the app is labelled in the admin.

A project with several apps

Output
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 lessons

What 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. progress may use courses, but courses shouldn't need to know about progress.
  • Reusable where possible. A well-designed app could move to another project. Many popular Django packages, like django-allauth for accounts, are just apps you install and add to INSTALLED_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.

Finished this lesson?Mark it complete to earn +20 XP and move your progress on.