๐Ÿ“ Django

Views in Django: Handling Requests

P
Author
PyLand Team
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
272
๐ŸŒฑ
Level
Beginner

A view is a function or class that receives an HTTP request and returns an HTTP response.

Function-Based View (FBV)

# tasks/views.py
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from .models import Task

def task_list(request):
    tasks = Task.objects.all()
    return render(request, 'tasks/list.html', {'tasks': tasks})

def task_detail(request, pk):
    task = get_object_or_404(Task, pk=pk)
    return render(request, 'tasks/detail.html', {'task': task})

HttpRequest โ€” the Request Object

def my_view(request):
    request.method          # 'GET', 'POST', 'PUT', 'DELETE'
    request.GET             # QueryDict with GET parameters (?q=hello)
    request.POST            # QueryDict with form data
    request.user            # current user
    request.path            # '/tasks/1/'
    request.META['HTTP_HOST']  # headers

HttpResponse and render

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect

# Simple response
def simple(request):
    return HttpResponse("Hello, World!")

# Render a template
def with_template(request):
    context = {'name': 'Django', 'version': 5}
    return render(request, 'myapp/page.html', context)

# Redirect
def after_action(request):
    return redirect('/tasks/')
    # or by URL name:
    return redirect('task-list')

Handling POST Requests

from django.views.decorators.http import require_http_methods

@require_http_methods(["GET", "POST"])
def create_task(request):
    if request.method == 'POST':
        title = request.POST.get('title', '').strip()
        if title:
            Task.objects.create(title=title)
            return redirect('task-list')

    return render(request, 'tasks/create.html')

get_object_or_404

from django.shortcuts import get_object_or_404

def task_detail(request, pk):
    # Instead of try/except Task.DoesNotExist
    task = get_object_or_404(Task, pk=pk)
    return render(request, 'tasks/detail.html', {'task': task})

If the object is not found, it automatically returns a 404 response.

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

settings.py: Django Configuration

settings.py is the central configuration file for a Django project.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 315
๐Ÿ“

CSRF Protection in Django

CSRF (Cross-Site Request Forgery) is an attack in which an adversary tricks the user's browser...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 294
๐Ÿ“

Django Migrations

A migration is a file describing changes to the database schema. Django tracks the state...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 285
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Django RequestLab: from HTTP to secure production Open course curriculum