How to Create an App in Django ?
In Django, an app is a web application that performs a specific functionality, such as blog posts, user authentication or comments. A single Django project can consist of multiple apps, each designed to handle a particular task. Each app is a modular and reusable component that includes everything needed for its specific feature like models, views, templates and URLs. For example, you might have a posts app for managing blog posts, a comments app for handling user feedback and a users app for login and registration. Let's explore the benefits of using Django apps:
Feature | Description |
---|---|
Modularity | Apps are loosely coupled and independently developed. |
Reusability | Apps are reusable across projects. |
Collaboration | Teams can work independently on separate apps. |
Organization | Maintains clean, organized, and manageable code. |
Built-in Features | Django offers tools like admin, middleware, etc., to accelerate development. |
Pre-installed apps in Django
Django provides some pre-installed apps for users. To see pre-installed apps, navigate to projectName –> projectName –> settings.py. In your settings.py file, you will find INSTALLED_APPS. Apps listed in INSTALLED_APPS are provided by Django for the developer's comfort.
Also, Visit :Django ORM – Inserting, Updating & Deleting Data
Before creating apps in django project we need to make sure that a project folder is already created, if it is then proceed with the steps below and if it isn't then create it using the following command:
django-admin startproject project_name
For a detailed understanding, refer to : How to Create a Basic Project using MVT in Django?
Below is the step by step guide to create apps in django:
Step 1: Navigate to the Project Directory
Open your terminal and move into the root directory of your Django project. this is where the manage.py file is located.
cd project_name
Step 2: Create a New App
You can create a Django app using the following command:
python manage.py startapp projectApp
This will generate a directory structure like:
Step 3: Register the App in Project's settings.py
Open project_name/settings.py and add your app to the INSTALLED_APPS list:
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'projectApp' # Add your app here
]
Step 4: Include App URLs in Project URLs
Now that we’ve created the app, we need to connect its URLs to the main project so that Django can properly route requests to the app’s views. Here's how to do it:
Open the urls.py file in your main project directory (project_name/projectApp).
Import include from django.urls at the top:
from django.urls import include
In the urlpatterns list, add the following line to include your app’s URLs:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls), # Admin panel URL
path('', include("projectApp.urls")), # Include app URLs
]
Step 5: Create urls.py in Your App
Your app needs its own urls.py to define the view routes. If it doesn’t exist yet, create it inside the projectApp directory:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
Step 6: Create a View
In projectApp/views.py, define a view that will return a simple response:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, Geeks! Welcome to your first Django app.")
Optional: Update ROOT_URLCONF (Not usually recommended)
By default, Django uses the project-level urls.py file as the main routing file (ROOT_URLCONF). It’s best to leave this setting as is, unless you have advanced needs.
From this:-
To this:
Step 7: Run the Development Server
With everything set up, it’s time to see your app in action. In the terminal, run:
python manage.py runserver
Then open your browser and visit:
http://127.0.0.1:8000/
You should see:
