Challenge - 5 Problems
Django Request-Response Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Django view when receiving a GET request?
Consider this Django view function. What will be the HTTP response content when a GET request is made to this view?
Django
from django.http import HttpResponse def my_view(request): if request.method == 'GET': return HttpResponse('Hello, GET request!') else: return HttpResponse('Not a GET request')
Attempts:
2 left
💡 Hint
Check the request.method attribute for GET requests.
✗ Incorrect
The view checks if the request method is 'GET'. If yes, it returns 'Hello, GET request!'. Otherwise, it returns 'Not a GET request'.
❓ state_output
intermediate2:00remaining
What is the value of response.status_code after this view runs?
Given this Django view, what will be the HTTP status code of the response object returned?
Django
from django.http import HttpResponse def status_view(request): response = HttpResponse('OK') response.status_code = 201 return response
Attempts:
2 left
💡 Hint
Look at how the status_code attribute is set on the response.
✗ Incorrect
The response status_code is explicitly set to 201 before returning, so the HTTP status code will be 201.
📝 Syntax
advanced2:00remaining
Which option correctly accesses POST data in a Django view?
You want to get the value of the 'username' field sent via POST in a Django view. Which code snippet correctly retrieves it?
Django
def post_view(request): # retrieve username from POST data pass
Attempts:
2 left
💡 Hint
POST data is accessed via request.POST dictionary.
✗ Incorrect
In Django, POST form data is accessed via request.POST dictionary. request.GET is for query parameters. request.body is raw bytes. request.data is not a Django attribute.
🔧 Debug
advanced2:00remaining
What error does this Django view raise when called?
Analyze this Django view code. What error will it raise when a request is made?
Django
from django.http import HttpResponse def error_view(request): return HttpResponse('Hello') + ' World'
Attempts:
2 left
💡 Hint
Check the types involved in the + operation.
✗ Incorrect
HttpResponse object cannot be concatenated with a string using + operator, causing a TypeError.
🧠 Conceptual
expert3:00remaining
Which option best describes how Django processes a request and returns a response?
Select the option that correctly describes the sequence Django follows when processing an HTTP request and returning a response.
Attempts:
2 left
💡 Hint
Think about the order of middleware and URL routing in Django's request cycle.
✗ Incorrect
Django first passes the request through middleware, then URL dispatcher selects the view, the view returns HttpResponse, then middleware processes the response before sending it back.