HttpRequest and HttpResponse
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).
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.
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.
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. #}
- Trying to read POST data with request.GET, or GET data with request.POST — they are separate dictionaries.
- Forgetting that request.GET and request.POST return QueryDict objects, not plain dicts (though they behave similarly).
- Returning a raw string from a view instead of wrapping it in HttpResponse, causing a TypeError.
- 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.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: