0
0
Djangoframework~20 mins

HttpRequest object in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HttpRequest Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Django view return?
Consider this Django view function. What will be the content of the HTTP response when a GET request is made with URL parameters ?name=Alice&age=30?
Django
from django.http import HttpResponse

def my_view(request):
    name = request.GET.get('name', 'Guest')
    age = request.GET.get('age', 'unknown')
    return HttpResponse(f"Name: {name}, Age: {age}")
AName: Alice, Age: 30
BName: Guest, Age: unknown
CName: Alice, Age: unknown
DName: Guest, Age: 30
Attempts:
2 left
💡 Hint
Look at how request.GET.get() fetches query parameters with defaults.
state_output
intermediate
1:30remaining
What is the value of request.method in this Django view?
If a client sends a POST request to this Django view, what will be the value of request.method inside the function?
Django
from django.http import HttpResponse

def sample_view(request):
    return HttpResponse(request.method)
A"DELETE"
B"PUT"
C"GET"
D"POST"
Attempts:
2 left
💡 Hint
request.method holds the HTTP method used by the client.
📝 Syntax
advanced
2:00remaining
Which option correctly accesses POST data in Django?
Given a Django view handling a form submission, which code correctly retrieves the value of the 'email' field from the POST data?
Django
from django.http import HttpResponse

def form_view(request):
    # retrieve email from POST data
    email = request.POST.get('email', '')
    return HttpResponse(email)
Arequest.POST.get('email', '')
Brequest.GET['email']
Crequest.POST['email']
Drequest.body['email']
Attempts:
2 left
💡 Hint
POST data is accessed via request.POST dictionary-like object. Use get() to avoid errors if key missing.
🔧 Debug
advanced
2:00remaining
Why does this Django view raise a KeyError?
This Django view raises a KeyError when the 'username' field is missing in the POST data. Why?
Django
from django.http import HttpResponse

def login_view(request):
    username = request.POST['username']
    return HttpResponse(f"User: {username}")
ABecause HttpResponse cannot handle formatted strings
BBecause request.method is not checked before accessing POST data
CBecause request.POST['username'] raises KeyError if 'username' is not in POST data
DBecause request.POST is empty for GET requests
Attempts:
2 left
💡 Hint
Accessing dictionary keys directly raises KeyError if key missing.
🧠 Conceptual
expert
2:30remaining
What is the type and content of request.body in Django?
In a Django view, what is the type of request.body and what does it contain?
Django
from django.http import HttpResponse

def api_view(request):
    data = request.body
    return HttpResponse(type(data).__name__)
Astr; decoded string of the request body
Bbytes; raw HTTP request body as bytes
Cdict; parsed JSON data from the request body
DNone; request.body is empty for GET requests
Attempts:
2 left
💡 Hint
request.body gives raw data sent by client, not parsed automatically.