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: