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:
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:
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 / classesTwo 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:
# 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:
# 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:
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"):
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:
# books/urls.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.book_list, name="book_list"),
]# 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?
- django-admin startproject books
- python manage.py startapp books
- python manage.py makeapp books
- pip install books
What must you do before Django notices a new app's models?
- Restart your computer
- Add the app to
INSTALLED_APPS - Create a superuser
- Run
collectstatic
What is the default database table name for model Book in app books?
- Book
- books
- books_book
- book_books