Others


Django - Create Django App


What is Django App

  • A Django app is a small, self-contained application within a Django project.
  • Apps are reusable components that can be used in multiple projects or within the same project.
  • Each app has its own models, views, templates, and static files.

Key Points of Django App

  • Modularity
  • Project Structure
  • Code Organization
  • Reusability
  • Settings

1. Create App

  • I will name my app store.
  • Navigate to the directory where your manage.py file is located.
python manage.py startapp store

Django creates a new store folder in ecommerce, with following content.

Django App Folder Content

2. Integrate the App into Your Django Project

To integrate the app in your project you need to specify your project name in 'INSTALLED_APPS' list in your project's settings.py file.

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

3. Create Views in App(store)

Define a view in the store app (store/views.py) to handle HTTP requests

from django.http import HttpResponse
from django.shortcuts import render

def home(request):
    return HttpResponse("Your app is working")

4. Setting up URLs in App(store)

Next, Create a urls.py file in your app directory (store/urls.py) and define URL patterns

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]

5. Add the App URLs in the Project (myproject)

Open myproject/urls.py inside the project directory and add the following code:

rom django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('store.urls')), # include the app URLs
]

6. Run the app

python manage.py runserver

After running the command, it will show the following result.

django app output

Prev