📝 Django

Views in Django: Handling Requests

P
Author
Pyland
📅
Published
30.06.2026
⏱️
Reading time
1 min
👁️
Views
81
🌱
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

📝

pytest-django: Testing Django

Охватываемые темы: Installation, @pytest.mark.djangodb, Fixtures, Testing views.

📅 30.06.2026 👁️ 134
📝

AI Agents: ReAct Loop and Autonomous Actions

A chatbot answers questions. An agent takes action: it calls tools, retrieves real data, and...

📅 30.06.2026 👁️ 100
📝

RAG: Chatting with Documents via Vector Search

RAG (Retrieval-Augmented Generation) is a pattern for working with your own documents. Instead of fine-tuning...

📅 30.06.2026 👁️ 94

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!