What is the purpose of the HttpResponse object in Django?
The HttpResponse object is used to send data back to the user's browser as a response to their request. It contains the content, status code, and headers of the HTTP response.
Click to reveal answer
beginner
How do you create a simple HttpResponse with the text 'Hello, world!' in Django?
You create it by importing <code>HttpResponse</code> from <code>django.http</code> and returning it from a view like this:<br><pre>from django.http import HttpResponse
def my_view(request):
return HttpResponse('Hello, world!')</pre>
Click to reveal answer
beginner
What is the default HTTP status code of an HttpResponse if you don't specify one?
The default HTTP status code is 200 OK, which means the request was successful.
Click to reveal answer
intermediate
How can you set a custom HTTP status code in an HttpResponse?
You can set it by passing the status parameter when creating the HttpResponse. For example:
HttpResponse('Not found', status=404)
Click to reveal answer
intermediate
How do you add custom headers to an HttpResponse object?
You add headers by treating the HttpResponse object like a dictionary. For example:
B. HttpResponse is not called as a function to create an instance
C. The content attribute cannot be set directly
D. HttpResponse must be imported from django.shortcuts
Solution
Step 1: Check how HttpResponse is assigned
The code assigns HttpResponse class itself to response instead of creating an instance by calling HttpResponse().
Step 2: Understand instance vs class usage
To set content, response must be an instance, so HttpResponse() must be called.
Final Answer:
HttpResponse is not called as a function to create an instance -> Option B
Quick Check:
HttpResponse() creates instance, not HttpResponse alone [OK]
Hint: Always call HttpResponse() to create an instance [OK]
Common Mistakes:
Assigning HttpResponse class instead of instance
Trying to set content on class, not instance
Importing HttpResponse from wrong module
5. You want to return a JSON response with content type set correctly using HttpResponse. Which code snippet correctly sets the content type to application/json?
hard
A. return HttpResponse('{"key": "value"}', content_type='application/json')
B. return HttpResponse('{"key": "value"}', type='application/json')
C. return HttpResponse.json('{"key": "value"}')
D. return HttpResponse('{"key": "value"}', mime='application/json')
Solution
Step 1: Recall how to set content type in HttpResponse
The correct parameter to set content type is content_type.
Step 2: Verify the options
Only return HttpResponse('{"key": "value"}', content_type='application/json') uses content_type='application/json' correctly; others use invalid parameters or methods.
Final Answer:
return HttpResponse('{"key": "value"}', content_type='application/json') -> Option A
Quick Check:
Use content_type='application/json' to set JSON response [OK]
Hint: Use content_type='application/json' to set JSON response [OK]
Common Mistakes:
Using wrong parameter names like type or mime
Trying to call non-existent .json() method
Not setting content type for JSON responses
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.