Part 4 - Apps in Django

October 18th, 2020 3 min read

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,

  1. πŸ”Œ 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.

  2. πŸ‘¨β€πŸ’» 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.

  3. 🐞 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:

  1. __init__.py : It is just to make folder act as a django package.
  2. admin.py : It is used to manage admin interface that django provides at application level.
  3. models.py : In this file we create models for our database. Here we structure our database in the django-way.
  4. tests.py : It is used to create unit tests for the app.
  5. views.py : It is where out views live. They determine how our app works. The are use to send responses to the requests.
  6. 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.


You may also like: