Django Apps

  • Learn how Django Apps work and how to create modular applications in a Django project.
  • Why Django Apps Are Important?

    In real projects:

    • A single website has multiple features

    • Each feature is built as a Django app

    👉 Apps make projects:

    • Modular

    • Reusable

    • Easy to maintain

    • Team-friendly

    Understanding apps is mandatory for real-world Django development.

    1. What is an App?

    Definition:

    A Django App is a self-contained module that performs a specific function in a project.

    👉 One app = One feature

    Real-Life Example:

    Website Feature

    Django App

    User login

    accounts app

    Blog system

    blog app

    Product listing

    products app

    Payments

    payments app

    Key Characteristics of an App:

    • Has its own models

    • Has its own views

    • Has its own URLs

    • Can be reused in other projects

    Code Example: Typical App Structure

    Code Title: Django App Folder Structure

    Code Description: Shows files created inside an app

    myapp/

    ├── admin.py

    ├── apps.py

    ├── models.py

    ├── views.py

    ├── tests.py

    ├── migrations/

    │   └── __init__.py

    2. Creating Apps

    When Do We Create an App?

    After:

    • Django project is created

    • Virtual environment is active

    Code Example: Creating a Django App

Create Django App

Creates a new app inside the project

python manage.py startapp myapp
  • App Created Successfully

    myapp/

    ├── admin.py

    ├── apps.py

    ├── models.py

    ├── views.py

    ├── migrations/


    Important Step: Register App in Project

    After creating an app, it must be registered in settings.py.

    Code Example: Register App

Add App to INSTALLED_APPS

Enables app in Django project

INSTALLED_APPS = [
    'myapp',
]
  • 3. Project vs App (Very Important Concept)

    What is a Project?

    A Django Project:

    • Is the main container

    • Controls settings and configurations

    • Can have multiple apps

    What is an App?

    A Django App:

    • Performs a specific function

    • Lives inside a project

    • Can be reused

    Project vs App Comparison

    Aspect

    Project

    App

    Purpose

    Website configuration

    Feature/module

    Contains

    Settings, URLs

    Models, Views

    Reusable

    Example

    E-commerce site

    Cart, Orders

    Real-World Analogy:

    Project = Shopping Mall
    App = Individual Shops

    Code Example: Multiple Apps in One Project

Project with Multiple Apps

Shows how apps work inside a project

ecommerce_project/
│
├── accounts/
├── products/
├── orders/
├── ecommerce_project/
├── manage.py
  • 4. How Apps Work Together

    Flow:

    1. User requests a URL

    2. Project routes URL

    3. App view handles logic

    4. App model interacts with database

    5. Template displays result

    Code Example: App View Example

Simple App View

Basic view inside an app

from django.http import HttpResponse

def home(request):
    return HttpResponse("Welcome to My App")