Create django app

Prerequisites

what is an app?

  • An app is a module in django project which represents a part of the overall project.
  • Let's say we are developing an e-commerce application then we devide project into apps like cart, payments, etc.

create an app

  • After creating the project the directory structure looks like below.
.
├── db.sqlite3
├── manage.py
└── my_project
    ├── __init__.py
    ├── asgi.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py
  • Run the command python manage.py startapp my_app to create the django app.
  • Now, the project structure looks like below.
.
├── db.sqlite3
├── manage.py
├── my_app
   ├── __init__.py
   ├── admin.py
   ├── apps.py
   ├── migrations
      └── __init__.py
   ├── models.py
   ├── tests.py
   └── views.py
└── my_project
    ├── __init__.py
    ├── asgi.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

django app structure

.
├── __init__.py
├── admin.py
├── apps.py
├── migrations
│   └── __init__.py
├── models.py
├── tests.py
└── views.py
  • __init__.py - It specifies the current directory as a python package
  • apps.py - It's the django app configuration file. It contains app name and other config.
  • models.py - It contains the ORM models, which represents the database tables.
  • migrations/ - It contains the django database migration files to update the database changes.
  • admin.py - Django comes with a powerful admin module. This file used to add the app models to admin.
  • tests.py - It contains the django unit tests.

add my_app to django project

  • open the project settings file my_project/settings.py and update the INSTALLED_APPS as below.
# ...
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # add your app
    'my_app',
]
# ...

References: