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: