How to create an app in Django

In web development, crafting outstanding applications demands a firm command of the available tools. This guide delves into Django, a Python web framework streamlining app creation. Before starting, it's vital to distinguish between a Django app and a Django project. Both are pivotal in Django's architecture, serving separate roles. Let's learn how to set up a Django app and comprehend its distinction from a Django project.

Django project vs. Django app

To understand Django's structure, it's crucial to differentiate between a project and an app:

Django project

  • Top-level structure that encompasses the entire application.
  • Contains settings, URLs, and project-wide configurations.
  • Can consist of multiple apps. Manages settings for the entire application.
  • Handles project-level templates and static files.

Django App

  • A component within a project, designed for a specific functionality.
  • Contains models, views, templates, and URLs for that functionality.
  • Can be reusable across projects. Has its own settings for that specific functionality.
  • Promotes modularity and organization within a project.

In simple terms, a project is like a bookshelf, while an app is like one of the books on that shelf.

Setting up the Django project

To learn how to set up a Django project, refer to this Answer.

Creating a Django app

Each Django project can contain multiple Django applications. These applications are where most of the functionality of the project lies. Once the project is set up, it's time to create an app:

Navigate to the project directory by using the following command:

cd projectname
Command to navigate to the project directory

Create a new Django app with the following command:

python manage.py startapp appname
Command to create a Django app

Remember to replace appname with the preferred name for the app.

App structure overview

The generated app directory will have several files and a sub-directory:

  • models.py: Defines the data models (database tables).

  • views.py: Handles the logic and rendering of responses.

  • urls.py: Contains URL patterns (not created by default, but can be added).

  • apps.py: Contains the app's configuration.

  • admin.py: Allows customization of the app's representation in the Django admin site.

  • migrations/: Stores migration files to keep track of changes to models.

Registering the app with the project

The new application has to be registered with the project. Open the file named 'settings.py' located inside the project directory. In this file, there's a list called INSTALLED_APPS. Add the name of the application to this list.

The updated INSTALLED_APPS will look like this:

INSTALLED_APPS = [
#....
#append the following line
'appname', # the app we created
]
Registering the app with the project

Remember to replace appname with the name of the application.

Building views in Django

The next step is building a view in the Django app. The purpose of a view is to render a template or to redirect to another view. Open the file 'views.py' located within the app directory and insert the following code:

from django.http import HttpResponse
from django.shortcuts import render
def home(request):
return HttpResponse("Welcome, This is Educative!")
Building views in Django

In the above code, a simple view named 'home' is created which returns an HttpResponse saying "Welcome, This is Educative!".

Setting up URLs

To make the view accessible through a browser, a URL needs to be associated with it. This can be done by creating a 'urls.py' file within the app directory and adding this code:

from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
Setting up URLs

This file essentially maps the URL to the view 'home'. The empty string indicates the root URL of this application.

Finally, add the app URLs to the project URLs. Open 'urls.py' inside the projectname directory and add this code:

from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('appname.urls')), # include the app URLs
]
Adding app URLs to the project URLs

Replace appname with the name of the app.

Running the app

The Django app is now ready. Run the following command to start the server:

python manage.py runserver
Command to start the server

Executable Django application

Here is the executable Django application. Click the "Run" button to execute the application.

Executable Django application

Wrapping up

By following this guide, a Django app is set up within a Django project. As the app develops, models can be defined, views can be expanded, and more functionality can be added. Remember, the power of Django apps lies in their reusability. Design them modularly, and they can become valuable components for multiple projects.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved