π 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_APPStells 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:
β Output:
Welcome to My First Django App
π§ Key Concepts
| Concept | Description |
|---|---|
| Project | Main container |
| App | Functional module |
| settings.py | Configuration file |
| INSTALLED_APPS | Registers apps |
| views.py | Logic |
| urls.py | Routing |
β οΈ 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
- Create app named
studentapp - Register it in settings.py
- Create view that displays βStudent App Workingβ
- 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
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
