Django

Create Django App and Register it in INSTALLED_APPS

πŸ“Œ Introduction

In Django, a project contains multiple apps, where each app handles a specific functionality.

In this program, you will:

  • Create a Django app
  • Understand its structure
  • Register it in INSTALLED_APPS

🎯 Program Statement

πŸ‘‰ Create a Django application and register it in INSTALLED_APPS.


βš™οΈ Step 1: Create Django Project (if not already created)

django-admin startproject myproject
cd myproject

βš™οΈ Step 2: Create Django App

python manage.py startapp myapp

πŸ“‚ Project Structure (After Creating App)

myproject/
β”‚
β”œβ”€β”€ manage.py
β”œβ”€β”€ myproject/
β”‚ β”œβ”€β”€ settings.py
β”‚ β”œβ”€β”€ urls.py
β”‚
└── myapp/
β”œβ”€β”€ admin.py
β”œβ”€β”€ apps.py
β”œβ”€β”€ models.py
β”œβ”€β”€ views.py

βš™οΈ Step 3: Register App in settings.py


πŸ“ File: myproject/settings.py

πŸ”Ή Path:

myproject/myproject/settings.py

πŸ”Ή Code (Add inside INSTALLED_APPS):

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

'myapp', # πŸ‘ˆ Add this line
]

🧠 Explanation

  • INSTALLED_APPS tells Django which apps are active
  • Without adding app here β†’ Django will ignore it

βš™οΈ Step 4: Verify App is Working

Run server:

python manage.py runserver

If no error β†’ app is successfully registered βœ…


βš™οΈ Step 5 (Optional but Recommended): Create Basic View


πŸ“ File: myapp/views.py

πŸ”Ή Path:

myproject/myapp/views.py

πŸ”Ή Code:

from django.http import HttpResponse

def home(request):
return HttpResponse("Welcome to My First Django App")

βš™οΈ Step 6: Map URL


πŸ“ File: myproject/urls.py

πŸ”Ή Path:

myproject/myproject/urls.py

πŸ”Ή Code:

from django.contrib import admin
from django.urls import path
from myapp import views

urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home),
]

🌐 Step 7: Run and Check Output

python manage.py runserver

Open browser:

πŸ‘‰ http://127.0.0.1:8000/

βœ… Output:

Welcome to My First Django App

🧠 Key Concepts

ConceptDescription
ProjectMain container
AppFunctional module
settings.pyConfiguration file
INSTALLED_APPSRegisters apps
views.pyLogic
urls.pyRouting

⚠️ Common Errors


❌ Error: ModuleNotFoundError: myapp

πŸ‘‰ Solution:

  • Check app name spelling
  • Ensure folder exists

❌ Error: View not found

πŸ‘‰ Solution:

  • Import correctly:
from myapp import views

❌ Forgot to register app

πŸ‘‰ Add:

'myapp'

in INSTALLED_APPS


πŸ§ͺ Practice Questions

  1. Create app named studentapp
  2. Register it in settings.py
  3. Create view that displays β€œStudent App Working”
  4. Map it to /student/ URL

🎀 Viva Questions


1. What is a Django app?

A Django app is a module that handles a specific functionality within a project. For example, authentication, blog, or student management can be separate apps.


2. Difference between project and app?

A project is the overall web application, while an app is a smaller component inside the project. Multiple apps can exist within a single project.


3. Why do we create apps in Django?

Apps help in organizing code into reusable modules. They make the project modular, scalable, and easier to maintain.


4. What is the use of startapp command?

The startapp command is used to create a new Django app. It generates the default structure like views, models, and admin files.


5. What is INSTALLED_APPS?

INSTALLED_APPS is a list in settings.py that tells Django which apps are active. Only registered apps are recognized and executed by Django.


6. What happens if an app is not added to INSTALLED_APPS?

Django will not recognize the app, and its models, views, and templates will not work. It may also cause errors during execution.


7. What is the role of views.py?

views.py contains functions that handle user requests and return responses. It acts as the logic layer of the application.


8. What is URL mapping?

URL mapping connects URLs to specific view functions. It ensures that the correct view is executed when a user visits a URL.


9. What is HttpResponse?

HttpResponse is used to send a response back to the browser. It can return text, HTML, or any data.


10. Why do we import views in urls.py?

We import views so that URL patterns can call the appropriate functions. Without importing, Django cannot link URLs to views.

🎀 Viva Questions


1. What is a Django app?

A Django app is a module that handles a specific functionality within a project. For example, authentication, blog, or student management can be separate apps.


2. Difference between project and app?

A project is the overall web application, while an app is a smaller component inside the project. Multiple apps can exist within a single project.


3. Why do we create apps in Django?

Apps help in organizing code into reusable modules. They make the project modular, scalable, and easier to maintain.


4. What is the use of startapp command?

The startapp command is used to create a new Django app. It generates the default structure like views, models, and admin files.


5. What is INSTALLED_APPS?

INSTALLED_APPS is a list in settings.py that tells Django which apps are active. Only registered apps are recognized and executed by Django.


6. What happens if an app is not added to INSTALLED_APPS?

Django will not recognize the app, and its models, views, and templates will not work. It may also cause errors during execution.


7. What is the role of views.py?

views.py contains functions that handle user requests and return responses. It acts as the logic layer of the application.


8. What is URL mapping?

URL mapping connects URLs to specific view functions. It ensures that the correct view is executed when a user visits a URL.


9. What is HttpResponse?

HttpResponse is used to send a response back to the browser. It can return text, HTML, or any data.


10. Why do we import views in urls.py?

We import views so that URL patterns can call the appropriate functions. Without importing, Django cannot link URLs to views.


πŸ‘‰ Next Post: Django Hello World View and URL Mapping
πŸ‘‰ Back to List: Django Programs (60 Questions with Solutions)


Further Reading

Introduction to Django Framework and its Features

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

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