← Back to Django Course | Chapter 2: Apps, URLs & Views | Lesson 9 of 10

HttpRequest and HttpResponse

HttpRequest is the letter a visitor sends to your website, and HttpResponse is the reply letter your website sends back.

Reading Data off HttpRequest

The request object passed into every view carries useful attributes: request.method (the HTTP verb), request.GET (query string parameters), request.POST (submitted form data), and request.user (the current user).

Example: Reading Data off HttpRequest

The request object passed into every view carries useful attributes: request.method (the HTTP verb), request.GET (query string parameters), request.POST (submitted form data), and request.user (the current user).

markup
from django.http import HttpResponse

def whoami(request):
    method = request.method
    name = request.GET.get('name', 'guest')
    return HttpResponse(f'{method} request from {name}')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Returning an HttpResponse

Every view must return some form of HttpResponse. The simplest case wraps plain text; Django also provides JsonResponse for JSON and HttpResponseRedirect for redirects.

Example: Returning an HttpResponse

Every view must return some form of HttpResponse. The simplest case wraps plain text; Django also provides JsonResponse for JSON and HttpResponseRedirect for redirects.

markup
from django.http import HttpResponse, JsonResponse

def status(request):
    return JsonResponse({'status': 'ok'})
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Checking the Request Method

Branching on request.method lets a single view handle both displaying a form (GET) and processing its submission (POST) differently.

Example: Checking the Request Method

Branching on request.method lets a single view handle both displaying a form (GET) and processing its submission (POST) differently.

markup
from django.http import HttpResponse

def submit(request):
    if request.method == 'POST':
        return HttpResponse('Form submitted!')
    return HttpResponse('Please submit the form.')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Trying to read POST data with request.GET, or GET data with request.POST — they are separate dictionaries.
  2. Forgetting that request.GET and request.POST return QueryDict objects, not plain dicts (though they behave similarly).
  3. Returning a raw string from a view instead of wrapping it in HttpResponse, causing a TypeError.
Chapter Summary
  • Every view receives an HttpRequest object carrying method, GET/POST data, headers, and the logged-in user.
  • Every view must return an HttpResponse object (or a subclass like JsonResponse or redirect).
  • request.method tells you whether the request was a GET, POST, or another HTTP verb.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.