Django apps

What are Django apps?

Django apps are a piece of code that does a specific function. By convention one app should perform one function, however, it depends on developer. Django apps are ways of creating web application in django. An app may be anything such as a blog app, a commenting system, alike button functionality app, etc.  Django since being a framework has certain conventions and rules that you need to follow in order to create a website. These conventions make the app reusable and secure.

Why create Django apps?

As already discussed Django apps are pieces of code. Not only that you can use one Django app created for one application into another app with minimal effort. Since Django lets you build applications faster, this feature helps you make reuse of the same app in multiple projects. This follows that the more you build apps the more development of another app with similar features becomes easier and faster.

How to create an app in Django? 

You can create apps in two ways, use any one of these

1. using django-admin command – with this command you can type as follows:

django-admin startapp <appname> 

for example, suppose you want to create a blog app, here is the command

django-admin startapp blog

2. using manage.py file – with this command you can write:

python manage.py startapp <app name>

for example, suppose you want to create a blog app, here is the command

python manage.py startapp blog

Which to use when?

You can use any one of these commands. For you manage.py command you have to be in the manage.py directory from terminal. If you want to separate apps by creating an apps/ directory and create all your apps inside that directory django-admin command is useful. 

Now that you have created blog app let’s also create contactus app,

For this type:

python manage.py startapp contactus

The folder structure of Django apps

Now that you have created two apps blog and contactus, let’s look at the folder structure and functionality of each file, 

As you can see both blog app and contactus have the same folders and files. Let’s look at individual folder and files in a single app, this is the same for all apps in Django. 

blog app contains:

1. migrations/ folder – This folder contains all your database mapping queries. For any dynamic application(which stores data and processes it) database is an essential part. The database need to have tables and rows and columns defined in order to create a table in them. In Django all of these are done in models.py file and when you perform python manage.py makemigrations a file is created in this migrations/ folder. “Migrations” folder contains by default an __init__.py file to make it a python package.

2. __init__.py file –  Each app is a python package that has this file. 

3. models.py file – This file contains all our models (schemas for creating tables and performing operations on the data). You will create models in the next chapter, for now, you understand that this file is related to the database. 

4. views.py – This file processes data and give an appropriate response to the calling browser.  What to render to a browse a simple text or HTML file or any other file is determined by this file. This file also connects the database with the URL request. 

5. admin.py file – Django comes with a build-in admin dashboard. If you want to show your created models in the admin panel you use this file. It let’s you register and structure you models in the admin pannel that Django provides out of the box. 

6. apps.py file – This file is used to configure your app settings. When you want to create your app as pluggable in the different project you have to modify settings here. 

7. tests.py – This file is used to write test cases for our application. Tests are important to any application, if you are building an app that is reliable you want to make sure you can test its feature. It should pass all the tests before you put your app into production(). 

Another file that is not present by default is urls.py. You may use your app to just process internal data or to do the cleanup in those cases urls.py file is not useful. This is the reason why apps in djanog don’t have urls.py file by default.

If you have multiple URLs you can add urls.py file and configure those in your app. 

You can also add various other python files and link them to your app. 

In the next post, you will learn how to create models and create database tables.

Leave a Reply

Your email address will not be published. Required fields are marked *