📝 Django

HttpRequest and HttpResponse in Django

P
Author
Pyland
📅
Published
30.06.2026
⏱️
Reading time
1 min
👁️
Views
76
🌱
Level
Beginner

Охватываемые темы: HttpRequest — the request object, HttpResponse — the response object, Redirect, render() — a convenient shortcut.

HttpRequest — the request object

def my_view(request):
    # Method
    request.method          # 'GET', 'POST', 'PUT', ...

    # Data
    request.GET             # QueryDict with GET parameters
    request.POST            # QueryDict with POST data
    request.FILES           # uploaded files
    request.body            # request body (bytes)

    # User
    request.user            # authenticated user
    request.user.is_authenticated

    # Metadata
    request.META['HTTP_HOST']           # domain
    request.META['REMOTE_ADDR']         # user's IP
    request.META['HTTP_USER_AGENT']     # browser
    request.path                        # '/tasks/1/'
    request.get_full_path()             # '/tasks/?status=done'

HttpResponse — the response object

from django.http import HttpResponse, JsonResponse

# Plain text
return HttpResponse('Hello, World!', status=200)

# HTML
return HttpResponse('<h1>Hello</h1>', content_type='text/html')

# JSON
return JsonResponse({'status': 'ok', 'count': 42})

# List
return JsonResponse([1, 2, 3], safe=False)

# Status codes
from django.http import HttpResponseNotFound, HttpResponseForbidden
return HttpResponseNotFound('Not found')
return HttpResponseForbidden('Access denied')

Redirect

from django.shortcuts import redirect
from django.urls import reverse

return redirect('task-list')                          # by URL name
return redirect('/tasks/')                            # by URL path
return redirect('task-detail', pk=task.pk)           # with arguments
return redirect(reverse('task-detail', kwargs={'pk': 1}))

render() — a convenient shortcut

from django.shortcuts import render

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

# Equivalent to:
from django.template.loader import render_to_string
html = render_to_string('tasks/list.html', {'tasks': tasks}, request=request)
return HttpResponse(html)

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 👁️ 132
📝

Django: Template Tags

Template tags are logic inside HTML. Unlike {{ variable }} which only outputs a value,...

📅 30.06.2026 👁️ 85
📝

Django: Static Files

Static files are CSS, JavaScript, images, and fonts. Django handles them in a specific way:...

📅 30.06.2026 👁️ 73

Did you like the article?

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