π Introduction
In Django, a view is a function that handles user requests and returns a response.
In this program, we will create a simple Hello World application and connect it with a URL.
π― Program Statement
π Create a simple Hello World view and map it to a URL.
βοΈ Step 1: Create Project and App
django-admin startproject myproject
cd myproject
python manage.py startapp myapp
βοΈ Step 2: Register App
π File: settings.py
πΉ Path:
myproject/myproject/settings.py
πΉ Code (Add this line):
INSTALLED_APPS = [
...
'myapp',
]
βοΈ Step 3: Create Hello World View
π File: views.py
πΉ Path:
myproject/myapp/views.py
πΉ Code:
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello World from Django!")
βοΈ Step 4: Configure URL Mapping
π File: 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('hello/', views.hello),
]
βοΈ Step 5: Run Server
python manage.py runserver
π Step 6: Check Output
Open browser:
π http://127.0.0.1:8000/hello/
β Output:
Hello World from Django!
π§ Explanation
- View β Function that returns response
- HttpResponse β Sends data to browser
- URL Mapping β Connects URL to view
- path(‘hello/’, views.hello) β Calls hello() when URL is accessed
π Alternative (Better Practice)
You can also make homepage:
path('', views.hello)
π Then open:
http://127.0.0.1:8000/
β οΈ Common Errors
β Error: view not found
π Ensure:
from myapp import views
β Error: Page not found (404)
π Check URL:
/hello/
(not /hello)
β Forgot to register app
π Add 'myapp' in INSTALLED_APPS
π§ͺ Practice Questions
- Create a view that displays βWelcome MCA Studentsβ
- Map it to
/welcome/ - Make it homepage
- Create two views and map different URLs
π€ Viva Questions & Answers
1. What is a view in Django?
A view is a Python function that takes a request and returns a response. It contains the logic of the application.
2. What is HttpResponse?
HttpResponse is used to send data back to the browser. It can return text, HTML, or JSON.
3. What is URL mapping?
URL mapping connects URLs to view functions. It determines which function runs for a given URL.
4. What is the role of path()?
The path() function defines URL patterns. It maps a URL route to a specific view.
5. What is request object?
The request object contains all information about the user request. It includes data like method, headers, and parameters.
6. What happens when a user enters a URL?
Django matches the URL with urls.py, calls the corresponding view, and returns a response to the browser.
7. Difference between function-based view and class-based view?
Function-based views use simple Python functions, while class-based views use classes and provide more reusable and structured code.
8. What is the default URL file in Django?
urls.py is the default file used for URL routing. It defines all URL patterns of the project.
9. Why do we import views in urls.py?
To connect URLs with the corresponding view functions. Without importing, mapping is not possible.
10. Can we map multiple URLs to same view?
Yes, multiple URL patterns can point to the same view. This allows flexibility in routing.
π Navigation
π Next Post: Create Multiple Pages (Home, About, Contact)
π 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
