Episode 5 of 32

Django Apps

Organize your project with Django apps — create a separate app for articles with its own models, views, URLs, and templates.

Django Apps

A Django app is a self-contained module that handles one part of your project. A project can have many apps — one for articles, one for accounts, one for an API, etc.

Creating an App

python manage.py startapp articles

App Structure

articles/
├── __init__.py
├── admin.py        ← Register models with the admin panel
├── apps.py         ← App configuration
├── migrations/     ← Database migration files
│   └── __init__.py
├── models.py       ← Database models
├── tests.py        ← Unit tests
└── views.py        ← View functions

Registering the App

# djangoblog/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'articles',   # Add your app here
]

App-Level URLs

# articles/urls.py (create this file)
from django.urls import path
from . import views

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

# djangoblog/urls.py (include the app URLs)
from django.urls import path, include

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

App Views

# articles/views.py
from django.shortcuts import render

def article_list(request):
    return render(request, 'articles/article_list.html')

Key Takeaways

  • Apps are modular — each handles one feature of your project
  • python manage.py startapp name creates a new app
  • Register apps in INSTALLED_APPS in settings.py
  • Use include() to connect app URLs to the project URL dispatcher