Django

Adding an Application

➕ Adding an Application in Django: Organizing Your Project with Apps

In Django, a project is the entire web application, while an app is a modular component within the project that handles a specific piece of functionality — like a blog, user management, or a store.

Creating and adding apps helps keep your project organized and scalable.

Let’s see how to add an app to your Django project!


️ Step 1: Create a New App

Run this command inside your Django project directory (where manage.py is):

bash
python manage.py startapp myapp

Replace myapp with your app’s name (e.g., blog, shop, accounts).

This creates a new folder named myapp with this structure:

markdown
myapp/
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
migrations/

Step 2: Register the App in settings.py

Open your project’s settings.py file and add your app to the INSTALLED_APPS list:

python
INSTALLED_APPS = [
# Default Django apps...
'django.contrib.admin',
'django.contrib.auth',
# Your app
'myapp',
]

This tells Django to include your app when running commands like migrations or starting the server.


Step 3: Create Views and URLs

Start building your app by defining views in myapp/views.py:

python
from django.http import HttpResponse

def home(request):
return HttpResponse("Welcome to MyApp!")

Next, create a urls.py file inside your app folder (myapp/urls.py):

python
from django.urls import path
from . import views

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


️ Step 4: Include App URLs in the Project

Open your project’s main urls.py file (e.g., myproject/urls.py) and include your app’s URLs:

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

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')), # Routes root URL to myapp
]


Step 5: Test Your App

Run the development server:

bash
python manage.py runserver

Visit http://127.0.0.1:8000/ in your browser. You should see:

css
Welcome to MyApp!

Summary Checklist

Step Command / Action
Create app python manage.py startapp myapp
Register app Add 'myapp' to INSTALLED_APPS
Define views Write functions in myapp/views.py
Add app URLs Create myapp/urls.py with URL patterns
Include app URLs Use include('myapp.urls') in project urls.py
Run server and test python manage.py runserver and visit site

Leave a Reply

Your email address will not be published. Required fields are marked *