Click to share! ⬇️

What Is A Django App

Django uses a modular structure of services to provide an overall solution. In other words, a Django project may have just a few, or in some cases a high number of different modules to provide the service that the project offers. In Django terminology, these modules are referred to as Apps. Each module, or App, has its own directory in the overall Django project. Any application code or Python files that provide the functionality for that App live in that specific directory. If this sounds confusing, don’t worry, Django has a feature to automate this process of creating an App, much like the automated process of creating a Project.


Pre Made Apps

If you want to see the variety of Apps you can use with your Django installation, you can check out the Django Packages website. Here, you will find more than 3000 different Apps to try out. Some of the popular categories for Apps include things like Activities, API Creation, Authentication, Blogs, Calendar, Chat, Custom Models, Developer Tools, and Feed Aggregators.


Custom Made Apps

In addition to all of the pre-made Apps that are available to try, we can build our own custom App in Django. You’ll need to know a little bit about Python and how the Web Platform works in order to do this. When you’re ready to create your own App in Django you can use a special command via the manage.py file.

python manage.py startapp appname

The startapp subcommand of manage.py is used to generate the scaffolding for a new App inside of the given Django Project. We want to create a new app, so let’s try that out now. We can call the App goals.

myproject $python manage.py startapp goals

When a new App is created with the startapp command, a new folder is added to the outer Django directory. We can see that new folder here in Visual Studio Code.

python django app folder

Inside of the new App directory are several more files and an additional inner directory.

django app file structure

Apps Live Inside The Project

A diagram of what this looks like is here.

django app inside django project

Let’s examine what each of these files does for us in an App for Django.

migrations folder – Each generated App in a Django project has its own migrations folder. Initially, this folder will have an empty __init__.py file, which is a regular package that is typically implemented as a directory containing an __init__.py file. When we start working with the database in Django, new Python files will be stored here that represent each Apps migrations. Migrations are used to define and update any changes that happened to Models. A Django Model is what Django uses to create tables, their fields, and various constraints.

__init__.py – Again, the __init__.py is simply to describe the current directory as a Python package or module.

admin.py

from django.contrib import admin

# Register your models here.

The admin.py file is used to register your models in the Django admin panel. The Django admin panel reads metadata from your models, such as fields, and lets you perform CRUD operations by simply configuring this admin.py file on a per App basis.

apps.py

from django.apps import AppConfig


class GoalsConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'goals'

The apps.py file helps the user include any application configuration for the given app. Using this, you can configure some of the attributes of the application. By default, the name variable is set to the same name given during App creation.

models.py

from django.db import models

# Create your models here.

We could argue that the models.py file is the most important of the files created for a Django App. It is in this file that you define the mapping of models to database tables.

tests.py

from django.test import TestCase

# Create your tests here.

The default startapp command creates a tests.py file in the new application. This is to help get you started with a place to put some basic tests for your App.

views.py

from django.shortcuts import render

# Create your views here.

The views.py file is probably the first file to begin editing or adding code to when a new Django app is created. The views.py is where Python functions are defined that accepts an HTTP request, and return an HTTP response. A View in Django can be thought of as a Controller in the MVC paradigm, though Django is itself an MVT(Model, View, Template) framework.

Learn More About Django Apps

Click to share! ⬇️