A View in Django is a Python function or class that receives a web request and returns a web response. Views contain the main logic that decides what should happen when a user visits a particular URL.
A view is a Python function or class that handles a request and returns a response.
def home(request):
return HttpResponse("Welcome to Django")
Here, home() is a Django view.
Views are used to define what should happen when a user accesses a URL.
Views are commonly written inside a file named views.py in a Django app.
myapp/
views.py
models.py
urls.py
To return a simple text response, import HttpResponse from Django.
from django.http import HttpResponse
This allows a view to send text back to the browser.
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello Django")
The home() function receives the request and returns a response.
A Django view normally receives a request object as its first parameter.
def home(request):
return HttpResponse("Welcome")
The request object contains information about the current browser request.
A view becomes accessible through a URL when it is connected in urls.py.
from django.urls import path
from . import views
urlpatterns = [
path('', views.home),
]
The basic flow is:
Browser
↓
URL
↓
URL Pattern
↓
View
↓
Response
↓
Browser
This is one of the most important concepts in Django.
def about(request):
return HttpResponse("About Us")
This view can be connected to an /about/ URL.
def contact(request):
return HttpResponse("Contact Us")
A view can contain different logic depending on the requirement of the page.
A Django application can contain many views.
def home(request):
return HttpResponse("Home")
def about(request):
return HttpResponse("About")
def contact(request):
return HttpResponse("Contact")
urlpatterns = [
path('', views.home),
path('about/', views.about),
path('contact/', views.contact),
]
Each URL can point to a different view.
A view can return HTML using HttpResponse.
def home(request):
return HttpResponse("<h1>Welcome</h1>")
For larger HTML pages, Django templates are normally used.
Django provides the render() function for returning an HTML template.
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
Templates keep HTML separate from Python view logic.
def home(request):
data = {
'name': 'Rahul'
}
return render(request, 'home.html', data)
The dictionary contains data that can be used inside the template.
A value passed from a view can be displayed using a template variable.
<h1>Hello {{ name }}</h1>
If the view passes name, Django can display its value.
Views can communicate with Django models to work with database information.
from .models import Student
def students(request):
data = Student.objects.all()
return render(request, 'students.html', {
'students': data
})
A view can perform operations such as:
Django ORM is commonly used for these operations.
Views can also return JSON responses, which are commonly used for APIs.
from django.http import JsonResponse
def data(request):
return JsonResponse({
'name': 'Rahul',
'age': 20
})
A view can redirect a user to another page using Django's redirect() function.
from django.shortcuts import redirect
def home(request):
return redirect('about')
Django supports both function-based views and class-based views.
from django.views import View
from django.http import HttpResponse
class HomeView(View):
def get(self, request):
return HttpResponse("Home Page")
A function-based view is a normal Python function that receives a request.
def home(request):
return HttpResponse("Home Page")
Function-based views are easy to understand and are useful for beginners.
Views can handle different types of HTTP requests, such as GET and POST.
def student(request):
if request.method == 'POST':
return HttpResponse("Data submitted")
return HttpResponse("Student Form")
A URL parameter can be passed to a view.
def student(request, id):
return HttpResponse(f"Student ID: {id}")
The URL pattern can provide the id value to the view.
path('student/<int:id>/', views.student)
For example, visiting /student/10/ can send 10 to the view.
A common Django page follows this workflow:
1. User visits URL
2. Django checks URL pattern
3. URL calls view
4. View processes request
5. View may access database
6. View renders template
7. Response goes to browser
request parameterHttpResponseurls.pyviews.py
from django.http import HttpResponse
def home(request):
return HttpResponse("Welcome to My Website")
def about(request):
return HttpResponse("About Us")
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home),
path('about/', views.about),
]
Views are one of the most important parts of a Django application. They connect URLs with application logic and generate responses for users.
views.py.HttpResponse can return a simple response.render() is used to display templates.Question: What is the main purpose of a Django view?