Bird
Raised Fist0
Djangoframework~20 mins

HttpResponse object in Django - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the primary purpose of the HttpResponse object in Django?
easy
A. To send data back to the browser as a response
B. To define database models
C. To handle user authentication
D. To manage URL routing

Solution

  1. Step 1: Understand the role of HttpResponse

    The HttpResponse object is designed to send data back to the browser after a request.
  2. Step 2: Compare with other Django components

    Other options like models, authentication, and routing serve different purposes unrelated to sending responses.
  3. Final Answer:

    To send data back to the browser as a response -> Option A
  4. Quick Check:

    HttpResponse sends response data [OK]
Hint: HttpResponse always sends data back to the browser [OK]
Common Mistakes:
  • Confusing HttpResponse with database models
  • Thinking HttpResponse manages URLs
  • Assuming HttpResponse handles user login
2. Which of the following is the correct way to create a simple HttpResponse with the text "Hello, World!" in Django?
easy
A. return HttpResponse.text('Hello, World!')
B. HttpResponse('Hello, World!').send()
C. return HttpResponse('Hello, World!')
D. HttpResponse.response('Hello, World!')

Solution

  1. Step 1: Recall HttpResponse constructor usage

    The HttpResponse object is created by calling HttpResponse() with the content string as an argument.
  2. Step 2: Check method correctness

    Only return HttpResponse('Hello, World!') correctly uses the constructor directly; other options use invalid methods.
  3. Final Answer:

    return HttpResponse('Hello, World!') -> Option C
  4. Quick Check:

    HttpResponse('text') creates response [OK]
Hint: Use HttpResponse('text') to create response quickly [OK]
Common Mistakes:
  • Using non-existent methods like .text() or .send()
  • Forgetting to return the HttpResponse object
  • Calling HttpResponse without parentheses
3. What will be the HTTP status code of this response?
from django.http import HttpResponse

def my_view(request):
    return HttpResponse('Not Found', status=404)
medium
A. 200
B. 302
C. 500
D. 404

Solution

  1. Step 1: Identify the status parameter in HttpResponse

    The HttpResponse is created with status=404, which sets the HTTP status code to 404.
  2. Step 2: Understand HTTP status codes

    Status 404 means "Not Found", matching the content and the status argument.
  3. Final Answer:

    404 -> Option D
  4. Quick Check:

    Status code set by status= parameter [OK]
Hint: Check the status= argument for HTTP status code [OK]
Common Mistakes:
  • Assuming default 200 status without checking status argument
  • Confusing 404 with 500 or 302 codes
  • Ignoring the status parameter in HttpResponse
4. Identify the error in this Django view code:
from django.http import HttpResponse

def view(request):
    response = HttpResponse
    response.content = 'Hello'
    return response
medium
A. The return statement is missing
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

  1. Step 1: Check how HttpResponse is assigned

    The code assigns HttpResponse class itself to response instead of creating an instance by calling HttpResponse().
  2. Step 2: Understand instance vs class usage

    To set content, response must be an instance, so HttpResponse() must be called.
  3. Final Answer:

    HttpResponse is not called as a function to create an instance -> Option B
  4. 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

  1. Step 1: Recall how to set content type in HttpResponse

    The correct parameter to set content type is content_type.
  2. 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.
  3. Final Answer:

    return HttpResponse('{"key": "value"}', content_type='application/json') -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard