Lesson 22 of 60 – Create Django App
37%

Create Django App

A Django project is usually divided into smaller components called applications. A Django app is designed to handle a particular part of a website, such as students, products, blogs, accounts, or courses.

Note: A Django project can contain multiple apps. For example, one project can have separate apps for students, courses, accounts, and blogs.

1. What is a Django App?

A Django app is a Python package that contains code for a specific feature or area of a Django project.

Examples include:

  • Student management
  • Blog system
  • User accounts
  • Product management
  • Course management

2. Project vs App

A project is the complete Django website configuration, while an app usually handles a specific feature or functionality.

Project
├── App 1
├── App 2
└── App 3

3. Why Create Apps?

Django apps help organize a large project into manageable parts.

  • Better code organization
  • Reusable functionality
  • Easier maintenance
  • Separate responsibilities
  • Better project structure

4. Create a Django App Command

The startapp management command is used to create a new Django app.

python manage.py startapp students

This command creates a new app named students.

5. Choosing an App Name

Choose a meaningful name that describes the purpose of the app.

python manage.py startapp students

python manage.py startapp courses

python manage.py startapp blog

python manage.py startapp accounts

Meaningful names make projects easier to understand.

6. Where to Run startapp?

Run the startapp command from the directory containing manage.py.

myproject/
├── manage.py
└── myproject/

Then run:

python manage.py startapp students

7. App Folder Created by Django

After running startapp, Django creates a folder for the app.

students/
├── __init__.py
├── admin.py
├── apps.py
├── migrations/
├── models.py
├── tests.py
└── views.py

8. __init__.py

The __init__.py file marks the directory as a Python package and allows Python to treat the app as a package.

students/
└── __init__.py

It is normally created automatically by Django.

9. views.py

The views.py file contains views that handle requests and return responses.

from django.http import HttpResponse

def home(request):
    return HttpResponse("Student App")

Views contain application logic related to the app.

10. models.py

The models.py file is used to define database models for the app.

from django.db import models

class Student(models.Model):
    name = models.CharField(max_length=100)

Models are used with Django's ORM to work with database records.

11. admin.py

The admin.py file is used to configure models for the Django admin site.

from django.contrib import admin
from .models import Student

admin.site.register(Student)

12. apps.py

The apps.py file contains configuration for the Django application.

from django.apps import AppConfig

class StudentsConfig(AppConfig):
    name = 'students'

Django creates this configuration automatically when the app is created.

13. migrations Folder

The migrations folder contains database migration files created by Django.

students/
└── migrations/
    └── __init__.py

Migration files record changes to database structure.

14. tests.py

The tests.py file can contain automated tests for the application.

from django.test import TestCase

class StudentTest(TestCase):
    pass

Testing helps verify that application functionality works as expected.

15. Registering the App

After creating an app, it should generally be added to INSTALLED_APPS when Django needs to discover its models, migrations, templates, or other app components.

INSTALLED_APPS = [
    ...
    'students',
]

16. Register App in settings.py

Open the project's settings.py file and add the app to INSTALLED_APPS.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'students',
]

17. Using AppConfig

The application configuration class can also be referenced in INSTALLED_APPS.

INSTALLED_APPS = [
    ...
    'students.apps.StudentsConfig',
]

This makes the app's configuration explicit.

18. Create a View in the App

Open students/views.py:

from django.http import HttpResponse

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

This creates a simple view for the students app.

19. Create urls.py for the App

Django does not automatically create an app-level urls.py when using startapp. We can create it manually.

students/
├── views.py
├── urls.py
└── models.py

The app's URL patterns can then be kept separate from the project's URLs.

20. App-Level urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home),
]

This connects the app's URL to its view.

21. Include App URLs

The project's main urls.py can include the app's URL patterns.

from django.contrib import admin
from django.urls import path, include

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

22. Testing the App URL

If the project includes the app using:

path('students/', include('students.urls'))

and the app contains:

path('', views.home)

then visiting the following URL can open the view:

/students/

23. Create a Model

An app can contain models that represent database tables.

from django.db import models

class Student(models.Model):

    name = models.CharField(
        max_length=100
    )

    email = models.EmailField()

24. Create Migrations

After creating or changing models, create migration files using:

python manage.py makemigrations

Django detects model changes and creates migration instructions.

25. Apply Migrations

Use the migrate command to apply migration changes to the database.

python manage.py migrate

This creates or updates database structures according to the migrations.

26. Multiple Apps in One Project

A single Django project can contain multiple apps.

myproject/
├── manage.py
├── myproject/
│
├── students/
├── courses/
├── accounts/
└── blog/

Each app can handle a different part of the website.

27. Reusable Django Apps

Django apps can be designed as reusable components.

For example, a blog app can potentially be reused in another Django project with appropriate configuration and dependencies.

  • Separate functionality
  • Reusable code
  • Clear structure
  • Easier maintenance

28. Common App Creation Mistakes

  • Running startapp outside the intended project directory
  • Forgetting to add the app to INSTALLED_APPS when required
  • Using the wrong app name
  • Forgetting to create or configure app URLs
  • Forgetting to include app URLs in the project URL configuration
  • Changing models without creating migrations
  • Forgetting to apply migrations

29. Complete Create App Example

Step 1: Create the app

python manage.py startapp students

Step 2: Add the app

INSTALLED_APPS = [
    ...
    'students',
]

Step 3: Create a view

from django.http import HttpResponse

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

Step 4: Create students/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home),
]

Step 5: Include app URLs

from django.urls import path, include

urlpatterns = [
    path(
        'students/',
        include('students.urls')
    ),
]

30. Summary of Creating a Django App

Creating apps is an important part of organizing Django projects. Each app can focus on a particular area of the website.

  • Use python manage.py startapp appname to create an app.
  • Django creates important files and folders automatically.
  • views.py contains views.
  • models.py contains database models.
  • admin.py configures the admin site.
  • apps.py contains app configuration.
  • migrations stores migration files.
  • Add the app to INSTALLED_APPS when required.
  • An app can have its own urls.py.
  • A project can contain multiple Django apps.

📌 Key Points

  • A Django app handles a specific area or feature of a project.
  • Use python manage.py startapp appname to create an app.
  • Django automatically creates files such as views.py, models.py, and admin.py.
  • Add the app to INSTALLED_APPS when its components need to be discovered by Django.
  • Apps can contain their own URL configuration.
  • Models require migrations when database structure changes.
  • A single project can contain many apps.
  • Apps help keep large Django projects organized and maintainable.

🧠 Quick Quiz

Question: Which command is used to create a new Django app?