This is part of the series Django For Beginners we are going to see about apps in app and we will see its project structure and we will see its philosophy and its advantages.
Django Project Structure
Django is famous for the way its projects are structured. It uses the concepts of apps in projects that makes its project struture so good. The structure of a django project is someting like this,
π project
- π project
- π app
- π app
- π app
- π app
- π app
Djangoβs philosophy behind this project structure.
Django expects the structure of an application in such a way that each functionality of project should be divided into app, letβs say you have a blog than blog ( posts part ), authintication part, user managment part should be all managed in diffrent apps.
Here are some of the benifits of using apps in django,
-
π Pluggable : Every function when segrated makes the app loosely coupled ( and sometimes independent ) and thus makes it easy to use in another projects. It makes the apps pluggable in other apps.
-
π¨βπ» Easy Development : At the same time diffrent developers can work on diffrent funtionalities for an project in form of apps and then they could be combined together easily, thus increasing efficiency.
-
π Easy Maintinanace : When we use the project is divided in apps it makes it easy to find and fix bugs as one can isolate an app and find and fix a bug.
** Fun Fact ** : Django itself is a collection of apps, it can be clear be seeing settings.py
file in django in INSTALLED_APPS
list.
In-build Django Apps
This brings our next topic, in-build
apps in django apps. Django comes out of box with some apps which are vital for its funtioning and add functionality to django. Some of the apps could be found in settings.py
of each django project.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
When you want to use an app in a project you need to add you app in this installed apps list.
Creating First Django App
To create a new app in a django project navigate to the root directory of that project and type the following command to create a new app,
python manage.py startapp myApp
Alternatively you can use,
django-admin startapp myApp
Exploring Structure Of A Django App
The structure of a django app is of this kind,
π myApp
- π migrations
- π __init__.py
- π __init__.py
- π admin.py
- π models.py
- π tests.py
- π views.py
- π apps.py
An typical django-app contains following files:
__init__.py
: It is just to make folder act as a django package.admin.py
: It is used to manage admin interface that django provides at application level.models.py
: In this file we create models for our database. Here we structure our database in the django-way.tests.py
: It is used to create unit tests for the app.views.py
: It is where out views live. They determine how our app works. The are use to send responses to the requests.apps.py
: It is a kind-of a configuration file for the application.
Hope you like it check another blog of this series below, also comment and tell me what do you feel about the post.