Django Create App
A Django project can contain many apps; this lesson shows how to generate a new app with startapp and register it so Django knows to use it.
Projects vs. Apps
In Django terminology, a project is the entire website: its settings, root URL configuration, and the collection of apps that make it work. An app is a self-contained Python package that implements one specific piece of functionality, such as a blog, a polls system, or user profiles. A single project commonly contains several apps, and a well-written app can even be reused across different projects.
- A project holds the global settings.py, the root urls.py, and the WSGI/ASGI entry points
- An app holds its own models.py, views.py, admin.py, migrations, and (optionally) templates and urls
- Apps are meant to be small and focused - 'one app, one job' keeps code reusable and testable
Creating an App with startapp
manage.py wraps django-admin and automatically points at your project's settings, so from inside a project directory you generate new apps with python manage.py startapp <name>. Django creates a new folder with a standard set of files ready to be filled in.
Example 1: Generate a New App
(env) $ python manage.py startapp polls
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.pyRegistering the App in INSTALLED_APPS
Creating the app's folder is not enough - Django only loads apps listed in the INSTALLED_APPS setting. Until an app is added there, migrations won't be generated for its models and its templates won't be discovered.
Example 2: Add the App in settings.py
# mysite/settings.py
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"polls", # newly created app
]Example 3: The Generated apps.py
# polls/apps.py
from django.apps import AppConfig
class PollsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "polls"Exercise: Django Create App
Which command generates a new Django app inside an existing project?