Challenge - 5 Problems
HttpRequest Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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}")
Attempts:
2 left
💡 Hint
Look at how request.GET.get() fetches query parameters with defaults.
✗ Incorrect
The request.GET dictionary contains URL query parameters. Using get('name', 'Guest') fetches 'Alice' from the URL, and similarly 'age' is '30'. So the response includes these values.
❓ state_output
intermediate1: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)
Attempts:
2 left
💡 Hint
request.method holds the HTTP method used by the client.
✗ Incorrect
The request.method attribute contains the HTTP method as a string. Since the client sends a POST request, request.method will be 'POST'.
📝 Syntax
advanced2: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)
Attempts:
2 left
💡 Hint
POST data is accessed via request.POST dictionary-like object. Use get() to avoid errors if key missing.
✗ Incorrect
request.POST is a dictionary-like object containing form data sent via POST. Using get('email', '') safely returns the value or empty string if missing.
🔧 Debug
advanced2: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}")
Attempts:
2 left
💡 Hint
Accessing dictionary keys directly raises KeyError if key missing.
✗ Incorrect
Using request.POST['username'] assumes the key exists. If missing, Python raises KeyError. Using get() avoids this error.
🧠 Conceptual
expert2: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__)
Attempts:
2 left
💡 Hint
request.body gives raw data sent by client, not parsed automatically.
✗ Incorrect
request.body is a bytes object containing the raw HTTP request body. It is not automatically decoded or parsed.