← Back to Chapters

Creating and Adding Django Apps

? Creating and Adding Django Apps

? Quick Overview

In Django, a project can consist of multiple apps, each of which handles a specific aspect of the project’s functionality. Apps are modular, allowing you to organize your project into reusable components.

? Key Concepts

  • Django apps are reusable modules inside a project
  • Each app focuses on a single responsibility
  • Apps must be registered in INSTALLED_APPS

? Syntax / Theory

Django provides the startapp command to generate the default app structure with essential files like models, views, and admin configuration.

? Step 1: Create a New Django App

? View Code Example
# Create a new app called 'myapp'
python manage.py startapp myapp

? Generated App Structure

? View Code Example
# Default files created inside the app
myapp/
__init__.py
admin.py
apps.py
models.py
tests.py
views.py

? Step 2: App Directory Explanation

  • __init__.py – Marks the folder as a Python package
  • admin.py – Register models for admin panel
  • apps.py – App configuration file
  • models.py – Database models
  • tests.py – Unit tests
  • views.py – Business logic

? Step 3: Add App to settings.py

? View Code Example
# Register app inside INSTALLED_APPS
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
]

? Step 4: Run Development Server

? View Code Example
# Start Django development server
python manage.py runserver

? Interactive Flow (Conceptual)

Project ➜ App ➜ Models ➜ Views ➜ URLs ➜ Browser

? Interactive App Simulator

See how views.py interacts with the browser. Change the text below:

? views.py
from django.http import HttpResponse
 
def index(request):
return HttpResponse("")
? Live Browser Output
Hello, Django!

? Use Cases

  • User authentication module
  • Blog system
  • E-commerce product module

✅ Tips & Best Practices

  • One app = one responsibility
  • Keep apps reusable across projects
  • Always register models in admin.py

? Try It Yourself

  • Create a new Django app
  • Add a model and migrate database
  • Create a view and map URL