Projects and Apps
Django uses two words that sound alike and mean different things. The project is the whole cookbook website. An app is one feature inside it, such as recipes. Keep them straight and every folder in a Django codebase makes sense.
The rule
One project per site; one app per feature.
With the environment active, create the project. The final dot means "put it in the current folder":
django-admin startproject cookbook .cookbook/ <- the folder you made earlier
├── manage.py <- the command tool for this project
└── cookbook/ <- the project's Python package
├── __init__.py
├── settings.py <- configuration for the whole site
├── urls.py <- the site's root table of URLs
├── asgi.py <- entry point for async servers
└── wsgi.py <- entry point for classic serversNow create an app for the first feature:
python manage.py startapp recipesrecipes/
├── __init__.py
├── admin.py <- show models in the admin site
├── apps.py <- the app's configuration class
├── migrations/ <- database change history
├── models.py <- data
├── tests.py <- tests
└── views.py <- request handlersAn app does nothing until you switch it on in settings.py:
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
# ...the other built-in apps...
"recipes",
]Notice that the built-in admin and login system are apps too. Django itself is assembled the same way you will build the cookbook.
The shortcut
Describe the feature in one sentence without the word "and". If you can, it is one app. "Recipes and reviews" becomes two apps: recipes and reviews. Each can be read, tested and even reused on its own.
The project
django-admin startproject cookbook .makesmanage.pyand thecookbooksettings package.
The most common slip: creating the app and forgetting INSTALLED_APPS. Django then ignores the app's models, and makemigrations reports "No changes detected".
Drills
Answer fast — trust the rule.
Which command creates a new app called reviews inside an existing project?
What does the trailing . in django-admin startproject cookbook . do?
You made the recipes app and wrote a model, but makemigrations says "No changes detected". Most likely cause?
Which file does startapp NOT create?
Remember
- One project per site; one app per feature.
startprojectonce,startappper feature, then add it toINSTALLED_APPS.- Trap: a forgotten
INSTALLED_APPSline makes the app invisible.
