TExcelTAD
Home › Django › Django Create App

Django Create App

A Django project is the whole website: settings, root URLs and deployment files. The actual features live in apps. An app is a Python package that does one job — a blog, a shop catalogue, a user profile area — and contains its own models, views, templates and tests. One project usually has several apps, and a well-written app can even be reused in other projects.

Project App
Created once with django-admin startproject Created many times with python manage.py startapp
Holds settings.py, root urls.py, wsgi.py Holds models.py, views.py, admin.py, tests.py
Represents the website Represents one feature of the website

Creating an app

Run startapp from the folder that contains manage.py:

bash · try it yourself
python manage.py startapp books
ls books
__init__.py  admin.py  apps.py  migrations  models.py  tests.py  views.py

The new package looks like this:

text
bookshelf/
├── manage.py
├── mysite/
│   ├── settings.py
│   └── urls.py
└── books/                 # the new app
    ├── __init__.py
    ├── admin.py           # register models with the admin site
    ├── apps.py            # the AppConfig class (name, label, ready hook)
    ├── migrations/        # migration files generated by makemigrations
    │   └── __init__.py
    ├── models.py          # database models
    ├── tests.py           # automated tests
    └── views.py           # view functions / classes

Two files you'll add yourself are very common: urls.py for the app's own URL patterns and a templates/books/ folder for its HTML.

Registering the app

Django ignores an app until you add it to INSTALLED_APPS in settings.py. Only then are its models, templates, admin registrations and migrations picked up:

python
# mysite/settings.py
INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "books",                      # our app
]

apps.py contains the app's configuration class, which startapp generates for you:

python
# books/apps.py
from django.apps import AppConfig

class BooksConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "books"

The app registry

After django.setup(), every installed app is described by an AppConfig object in the app registry, django.apps.apps. You can explore it from Python — here with Django's own built-in apps, since a playground has no app folder of its own:

python · try it yourself
import django
from django.conf import settings
settings.configure(INSTALLED_APPS=[
    "django.contrib.contenttypes",
    "django.contrib.auth",
    "django.contrib.sessions",
])
django.setup()
from django.apps import apps

for config in apps.get_app_configs():
    print(f"{config.label:<14} {config.name:<28} {config.verbose_name}")

print(apps.is_installed("django.contrib.auth"))
print(apps.is_installed("books"))
contenttypes   django.contrib.contenttypes  Content Types
auth           django.contrib.auth          Authentication and Authorization
sessions       django.contrib.sessions      Sessions
True
False
AppConfig attribute Meaning
name full Python path of the package, e.g. "books" or "django.contrib.auth"
label short unique name, used in table names (books_book) and app_label.Model strings
verbose_name human-readable name shown in the admin
ready() method called once at start-up — the place to connect signals

Models belong to apps

Each app owns its models, and the registry lets you look them up by "app_label.ModelName". That's how Django resolves string references such as ForeignKey("auth.User"):

python · try it yourself
import django
from django.conf import settings
settings.configure(INSTALLED_APPS=["django.contrib.contenttypes", "django.contrib.auth"])
django.setup()
from django.apps import apps

auth = apps.get_app_config("auth")
print([m.__name__ for m in auth.get_models()])

User = apps.get_model("auth.User")
print(User._meta.db_table)
print(User._meta.app_label)
['Permission', 'Group', 'User']
auth_user
auth

In playground examples a model sets class Meta: app_label = "demo" because it isn't inside an app package — the result is the same as if you'd written it in demo/models.py.

Connecting the app's URLs

A common pattern is to give each app its own urls.py and include it from the project's root URLconf:

python
# books/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path("", views.book_list, name="book_list"),
]
python
# mysite/urls.py
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path("admin/", admin.site.urls),
    path("books/", include("books.urls")),
]

Now /books/ is handled by books.views.book_list. You'll learn the details in Django URLs.

Tip: Keep apps small and focused. If you can't describe an app in one sentence ("manages the book catalogue"), it probably wants to be split.

Test yourself

Which command creates a new app called books?

  1. django-admin startproject books
  2. python manage.py startapp books
  3. python manage.py makeapp books
  4. pip install books

What must you do before Django notices a new app's models?

  1. Restart your computer
  2. Add the app to INSTALLED_APPS
  3. Create a superuser
  4. Run collectstatic

What is the default database table name for model Book in app books?

  1. Book
  2. books
  3. books_book
  4. book_books