How to create Django Applications

Within Django project, we can have multiple apps that have different functions.

For example, we can have a Blog section and a Store section for our site. There is a different app for each task.

In this tutorial, we’ll learn

  • Django app basic info
  • Create Django application
  • Install new app

I. Benefit of Django apps

There are several benefits of creating an app

  • An app acts as a completely independent module.
  • Each app has in-built features such as admin pages, so we don’t have to build things from stratch
  • Django apps are reusable. If we prefer a certain app, we can also add it to other projects.
  • It’s easier to debug and organize our code

II. Pre-installed Django apps

Django provides some pre-installed apps for users. To check them, we can navigate to projectName –> settings.py.

In settings.py file, you will find INSTALLED_APPS.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

III. Create an application in Django

To create an application, we run python manage.py startapp APP_NAME. This will create some additional directories and files.

For example, in mysite directory, we run python manage.py startapp blog.

We can view the directory structure with tree command:

.
├── blog
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── db.sqlite3
├── manage.py
└── mysite
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-39.pyc
    │   ├── settings.cpython-39.pyc
    │   ├── urls.cpython-39.pyc
    │   └── wsgi.cpython-39.pyc
    ├── asgi.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

IV. Install new app

To install our new app in this project, go to projectName -> settings.py, scroll down to the list of INSTALLED_APPS.

We then add the name of our new application to this list.

INSTALLED_APPS = [
    'blog.apps.BlogConfig', # Add blog app
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

IV. Render an app

We’ll then need to render an app. This URL will tell which URL will send us to our app.

  1. Navigate to projectName -> urls.py
  2. Import include function from django.urls in the header.
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('blog/', include("blog.urls"))
]

Note: If you’d like your Blog page to be your front page, you can leave blank.

For example: path('', include("blog.urls")).

VI. Summary on creating Django app

Here is a brief summary on how to create a new app named blog

  1. Run python manage.py startapp blog in the terminal to create a new app.

  2. To add our app to the project, edit project’s settings.py, add “blog” as one of our INSTALLED_APPS.

INSTALLED_APPS = [
    'blog.apps.BlogConfig'
]
  1. Edit our project’s urls.py file, and include a path
   path('blog/', include("blog.urls"))

Next, check out Django views.