Views in Django
- Learn how Django views handle user requests and return responses in web applications.
What is a View in Django?
A View is a Python function or class that:
Receives a request from the browser
Processes data (logic)
Returns a response (HTML, text, JSON, etc.)
In simple words:
👉 View = Brain of Django application🔄 Role of View in MVT Architecture
📌 View connects Model and Template
🔹 Types of Views in Django
Django provides two main types of views:
Function-Based Views (FBV)
Class-Based Views (CBV)
Function-Based Views (FBV)
What is Function-Based View?
A Python function
Takes an HTTP request
Returns an HTTP response
✔ Simple
✔ Easy to understand
✔ Best for beginners
Basic Syntax
def view_name(request):
return HttpResponse("Response")
Simple FBV
Basic Function-Based View
This view returns a simple text response to the browser.
from django.http import HttpResponse
def home(request):
return HttpResponse("Welcome to Django Home Page")
When user visits URL → browser shows text
FBV with HTML
Returning HTML using FBV
This view sends HTML content as response.
def about(request):
return HttpResponse("<h1>About Django</h1><p>Django is a web framework</p>")
FBV with URL Mapping
Connecting View with URL [ urls.py ]
Maps URL to function-based view.
from django.urls import path
from . import views
urlpatterns = [
path('', views.home),
path('about/', views.about),
]
Advantages of FBV
Easy to learn
Good for small projects
Clear flow of logic
Limitations of FBV
❌ Code repetition
❌ Hard to manage large applicationsClass-Based Views (CBV)
What is Class-Based View?
View written as a Python class
Uses OOP concepts
Each HTTP method handled separately
Example:
GET
POST
PUT
DELETE
Why CBV?
Reusable code
Cleaner structure
Better for large applications
Basic Syntax
from django.views import View
class MyView(View):
def get(self, request):
return HttpResponse("Hello")
Code Example : Simple CBV
Basic Class-Based View
Handles GET request using class.
from django.http import HttpResponse
from django.views import View
class HomeView(View):
def get(self, request):
return HttpResponse("Welcome to Class Based View")
URL Mapping for CBV
Mapping CBV to URL
Use .as_view() to connect CBV.
from django.urls import path
from .views import HomeView
urlpatterns = [
path('', HomeView.as_view()),
]
.as_view() converts class into callable view
CBV with GET & POST
Handling Multiple HTTP Methods
Same view handles both GET and POST.
class ContactView(View):
def get(self, request):
return HttpResponse("Contact Page")
def post(self, request):
return HttpResponse("Form Submitted")
Advantages of CBV
Code reuse
Less duplication
Professional structure
HttpResponse in Django
What is HttpResponse?
Sends response to browser
Can return:
Text
HTML
JSON
File
HttpResponse with Text
from django.http import HttpResponse
def message(request):
return HttpResponse("Hello Django")
HttpResponse with HTML
def html_view(request):
return HttpResponse("<h2>Django Response</h2>")
HttpResponse with JSON
from django.http import JsonResponse
def json_view(request):
return JsonResponse({"name": "Django", "type": "Framework"})