Creating an Application in Django
In Django, an application (app) is a self-contained module that implements specific website functionality.Apps help organize project code into reusable, modular components.
For instance, you might create an "articles" app for blog posts, a "users" app for authentication, or a "shop" app for e-commerce features.Each app manages its own data models, views, templates, and related resources.
This modular approach mirrors how frontend developers organize projects into feature-based folders.
Creating an App
Use the startapp management command to generate a new application:
python manage.py startapp articles
This creates an articles directory with the following structure:
articles/
├── __init__.py
├── admin.py
├── apps.py
├── migrations/
├── models.py
├── tests.py
└── views.py
Each file serves a specific purpose:
models.py- defines database schemasviews.py- handles request processingadmin.py- configures admin interface
Creating a View
Open articles/views.py and add a simple view function:
from django.http import HttpResponse
def article_list(request):
return HttpResponse('Articles Index Page')
This vieew returns a simple HTTP response when accessed.
Configuring URLs
Register the view in you're project's URL configuration.Open demo1/urls.py:
from django.contrib import admin
from django.urls import path
from articles.views import article_list
urlpatterns = [
path('admin/', admin.site.urls),
path('articles/', article_list),
]
The path() function maps a URL pattern to a view function.
Testing
Run the development server:
python manage.py runserver
Navigate to http://127.0.0.1:8000/articles/ to see the response.
Registering the App
Before using the app, add it to INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'articles',
]
This registers the app with Django's project system.