Views are one of the most important parts of a Django application. They contain the logic that processes user requests and returns responses. In simple terms, views are the “brain” of your web application.
In this tutorial, you will learn what Django views are, how they work, how to create them, and how to return different types of responses.
A view is a Python function or class that receives a request and returns a response.
User Request → View → ResponseThe response can be:
When a user visits a URL:
urls.pySo, views connect URLs to actual functionality.
Open your app file:
blog/views.pyfrom django.http import HttpResponse
def home(request):
return HttpResponse("Hello, this is my first view!")request → contains information about the user requestHttpResponse() → sends a response back to the browserIn blog/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]Now when you visit:
You will see:
Hello, this is my first view!
request ObjectEvery view receives a parameter called request.
def home(request):def info(request):
return HttpResponse(f"Method: {request.method}")render()Most of the time, you don’t return plain text. You return HTML pages.
render()from django.shortcuts import render
def home(request):
return render(request, 'blog/home.html')render() doesYou can send data from views to templates using a context dictionary.
def home(request):
context = {
'name': 'Youssef',
'age': 25
}
return render(request, 'blog/home.html', context)home.html)<h1>Hello {{ name }}</h1>
<p>Age: {{ age }}</p>Views can receive parameters from URLs.
urls.pypath('post/<int:id>/', views.post_detail)views.pydef post_detail(request, id):
return HttpResponse(f"Post ID: {id}")/post/5/ → "Post ID: 5"Django views can return different types of responses.
from django.http import HttpResponse
return HttpResponse("Hello World")from django.http import JsonResponse
def api(request):
data = {'name': 'Django', 'type': 'framework'}
return JsonResponse(data)from django.shortcuts import redirect
def go_home(request):
return redirect('home')from django.http import FileResponseUsed for downloading files.
Views can behave differently depending on the request type.
def example(request):
if request.method == 'GET':
return HttpResponse("This is a GET request")
elif request.method == 'POST':
return HttpResponse("This is a POST request")When a user submits a form, data is available in:
request.POSTdef submit(request):
name = request.POST.get('name')
return HttpResponse(f"Hello {name}")You can define multiple views in views.py.
def home(request):
return HttpResponse("Home")
def about(request):
return HttpResponse("About")
def contact(request):
return HttpResponse("Contact")
urls.pypath('', views.home),
path('about/', views.about),
path('contact/', views.contact),def home(request):
return HttpResponse("Hello")from django.views import View
class HomeView(View):
def get(self, request):
return HttpResponse("Hello")Start with function-based views.
Later, you can learn class-based views.
def post_list(request):
posts = Post.objects.all()
return render(request, 'blog/list.html', {'posts': posts})from django.http import HttpResponse
render(request, 'home.html') ❌
render(request, 'blog/home.html') ✅
def home(request):
HttpResponse("Hello") ❌Must be:
return HttpResponse("Hello") ✅Always check urls.py.
views.pyfrom django.shortcuts import render
def home(request):
posts = [
{'title': 'Post 1'},
{'title': 'Post 2'},
]
return render(request, 'blog/home.html', {'posts': posts})home.html<h1>Blog</h1>
<ul>
{% for post in posts %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>Think of a Django view as a waiter in a restaurant.
In this tutorial, you learned that Django views are responsible for handling requests and returning responses. You created simple views, connected them to URLs, rendered templates, passed data, and handled different types of responses.
Views are a core part of Django and are essential for building dynamic applications.
HttpResponse sends basic responsesrender() returns HTML templatesrequest contains user datarender() do?request object?Django views are where your application logic lives. Once you understand views, you can start building real features like blogs, dashboards, and APIs.