Creating First Django App In Django, features are organized into modular, decoupled applications. When you create an app, Django scaffolds a standardized directory structure containing models, views, tests, and admin configurations. Learning how to create, register, and wire an app into the URL routing pipeline is the primary workflow of Django development.
To create a new app named articles:
1 python manage.py startapp articles
Django generates the following files:
1 articles/
2 ├── __init__.py
3 ├── admin.py # Registration of models with Django Admin dashboard
4 ├── apps.py # App configuration metadata
5 ├── migrations/ # Auto-generated database migration scripts
6 │ └── __init__.py
7 ├── models.py # Data models and database schemas
8 ├── tests.py # Unit and integration test suites
9 └── views.py # Request handling and HTTP response logic
Django will not detect your app's models, migrations, or templates until it is registered in settings.py:
1 # config/settings.py
2
3 INSTALLED_APPS = [
4 # Core apps...
5 "django.contrib.admin" ,
6 "django.contrib.auth" ,
7 "django.contrib.contenttypes" ,
8 "django.contrib.sessions" ,
9 "django.contrib.messages" ,
10 "django.contrib.staticfiles" ,
11
12 # Custom Domain Apps
13 "articles.apps.ArticlesConfig" , # Or simply 'articles'
14 ]
A Django view is a Python callable (function or class) that takes an HttpRequest object and returns an HttpResponse object.
1 # articles/views.py
2 from django . http import HttpResponse , JsonResponse
3
4 def home_view ( request ) :
5 return HttpResponse ( "<h1>Welcome to Enterprise Django Platform</h1>" )
6
7 def health_check ( request ) :
8 return JsonResponse ( {
9 "status" : "healthy" ,
10 "service" : "articles-api" ,
11 "version" : "1.0.0"
12 } )
4. App-Level URL Routing (articles/urls.py) Create a dedicated urls.py inside the articles/ directory:
1 # articles/urls.py
2 from django . urls import path
3 from . import views
4
5 # Application namespace for reverse URL resolution
6 app_name = "articles"
7
8 urlpatterns = [
9 path ( "" , views . home_view , name = "home" ) ,
10 path ( "health/" , views . health_check , name = "health-check" ) ,
11 ]
Connect the app's URLs to the root config/urls.py:
1 # config/urls.py
2 from django . contrib import admin
3 from django . urls import path , include
4
5 urlpatterns = [
6 path ( "admin/" , admin . site . urls ) ,
7 path ( "articles/" , include ( "articles.urls" ) ) ,
8 ]
Now visiting http://127.0.0.1:8000/articles/health/ returns the JSON payload:
1 {
2 "status" : "healthy" ,
3 "service" : "articles-api" ,
4 "version" : "1.0.0"
5 }
Interactive Quiz • Knowledge Check
Practice Quiz What command scaffolds a new application module in a Django project?
A django-admin create-app <name>
B python manage.py startapp <name>
C python manage.py new-module <name>
D npm init django-app
What happens if you forget to add a newly created app to INSTALLED_APPS in settings.py?
A The computer displays a blue screen
B Django will not discover the app's models, database migrations will not be generated, and templates or admin registrations will be ignored
C Django deletes the app folder automatically
D Python throws an IndentationError
What is the fundamental contract of a Django view function?
A It must accept an HttpRequest object as its first parameter and return an HttpResponse object
B It must return a SQL query string
C It must return void
D It must take a CSS file and compile it
What is the purpose of setting app_name = "articles" in an app's urls.py?
A It changes the database name
B It establishes an application URL namespace, enabling unambiguous reverse URL lookups via reverse('articles:home')
C It sets the browser tab title
D It encrypts the route URLs
In which file should you define database tables and entity schemas for an app?
A views.py
B admin.py
C models.py
D urls.py