Creating Your First Django App
What is a Django App?
In Django, apps are individual components that help you structure your project into reusable modules. Each app typically handles a specific functionality, such as a blog, user authentication, or a contact form.
Creating a Django App:
Django allows you to modularize your project using apps. Follow the steps below to create an app:
Create an App in the Project Directory
- Open your terminal or command prompt in the root of your Django project.
- Run the following command to create a new app:
python manage.py startapp app_name
Create an App Inside an `apps` Folder
For better project organization, especially in large projects, it's recommended to place all apps inside a dedicated apps folder.
- Inside your Django project directory, create a folder named apps.
- Add an empty `__init__.py` file inside it to mark it as a Python package.
- Then run the command below to create your app inside the `apps` folder:
python manage.py startapp core apps/core
Project structure with apps
apps/core: This directory contains essential app files like apps.py, models.py, views.py, and admin.py, which define the structure and logic of the core app.
manage.py: The command-line utility used to interact with the Django project.
django_tutorials: The main project folder that holds global settings and configurations.
manage.py
│
├───apps
│ └───core
│ ├── admin.py # Registers models with Django admin
│ ├── apps.py # App configuration
│ ├── models.py # Defines the models (database structure)
│ ├── tests.py # Contains unit tests for the app
│ ├── views.py # Views handling requests
│ └── __init__.py # Marks this directory as a Python package
│
│
└───django_tutorials [Project Folder]
Adding an App to Django's Installed Apps
In Django, apps are added to the project by listing them in the INSTALLED_APPS in settings.py, using their Python module path (e.g., 'myapp'). This tells Django which apps to manage for the project.
Adding a Regular App:
If you create an app directly inside your project (e.g., myapp), add it to INSTALLED_APPS like this:
# Add the app to INSTALLED_APPS in project/settings.py
INSTALLED_APPS = [
...
'myapp',
]
Adding an App from `apps/core` Path:
If your app is located in a nested folder like apps/core, follow these two steps:
Step-1: Add the app path to INSTALLED_APPS in project/settings.py:
# Add the app to INSTALLED_APPS in project/settings.py
INSTALLED_APPS = [
...
'apps.core',
]
Step-2: In apps/core/apps.py, update the name attribute to match the full path:
# apps/core/apps.py
from django.apps import AppConfig
class CoreConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'apps.core'
Exercise
- Fill in the Blanks:
- - In Django, an app is a __________ that handles a specific functionality in the project.
- - To create a new app in Django, you use the command __________.
- - The command to create an app inside the `apps` folder is __________.
- - The app configuration, including its name and settings, is in the __________ file.
- - The `__init__.py` file inside the app directory marks it as a __________.