0
0
Djangoframework~20 mins

HttpResponse object in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HttpResponse 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 is the output when a client requests this view?
Django
from django.http import HttpResponse

def my_view(request):
    response = HttpResponse('Hello World', content_type='text/plain')
    response.status_code = 201
    return response
AA JSON response with body 'Hello World' and HTTP status 200
BA plain text response with body 'Hello World' and HTTP status 201
CA plain text response with body 'Hello World' and HTTP status 200
DA plain text response with empty body and HTTP status 201
Attempts:
2 left
💡 Hint
Look at the content_type and status_code set on the HttpResponse object.
📝 Syntax
intermediate
2:00remaining
Which option correctly creates an HttpResponse with a custom header?
You want to return an HttpResponse with the header 'X-Custom: 123'. Which code snippet does this correctly?
A
response = HttpResponse('data')
response.set_header('X-Custom', '123')
Bresponse = HttpResponse('data', headers={'X-Custom': 123})
C
response = HttpResponse('data')
response.headers['X-Custom'] = '123'
D
response = HttpResponse('data')
response['X-Custom'] = '123'
Attempts:
2 left
💡 Hint
HttpResponse headers are set like dictionary keys on the response object.
state_output
advanced
2:00remaining
What is the content of this HttpResponse after modification?
Given this code, what will be the final content of the HttpResponse object?
Django
response = HttpResponse('Start')
response.content += b' Middle'
response.content = response.content.replace(b'Start', b'Begin')
response.content += b' End'
Ab'Begin Middle End'
Bb'Start Middle End'
Cb'Begin Middle'
Db'Start Middle'
Attempts:
2 left
💡 Hint
HttpResponse.content is bytes, so string operations must use bytes.
🔧 Debug
advanced
2:00remaining
Why does this HttpResponse raise an error?
This code raises an error. What is the cause?
Django
response = HttpResponse()
response.content = 'Hello World'
print(response.content)
AValueError because content cannot be empty
BAttributeError because content attribute is read-only
CTypeError because HttpResponse.content expects bytes, not str
DNo error, prints 'Hello World'
Attempts:
2 left
💡 Hint
HttpResponse.content expects bytes, not a string.
🧠 Conceptual
expert
2:00remaining
What happens if you set HttpResponse.status_code to 404 after returning it?
If you create an HttpResponse and return it from a view, then later in middleware you set response.status_code = 404, what will the client receive?
AThe client receives the response with status code 404
BThe client receives the original status code set in the view
CThe client receives a 500 Internal Server Error
DThe status code change is ignored and client receives 200
Attempts:
2 left
💡 Hint
HttpResponse objects are mutable until sent to the client.