Recall & Review
beginner
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:<br>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:<br>response = HttpResponse('Hello')
response['X-Custom-Header'] = 'MyValue'Click to reveal answer
What does the
HttpResponse object represent in Django?✗ Incorrect
The
HttpResponse object represents the response Django sends back to the user's browser.Which module do you import
HttpResponse from?✗ Incorrect
You import
HttpResponse from django.http.What is the default status code of an
HttpResponse if not specified?✗ Incorrect
The default status code is 200, meaning the request was successful.
How do you set a 404 status code in an
HttpResponse?✗ Incorrect
You set the status code by passing the
status parameter.How can you add a custom header 'X-Test' with value '123' to an
HttpResponse?✗ Incorrect
You add headers by assigning to the response object like a dictionary.
Explain how to create and return a simple HttpResponse in a Django view.
Think about the minimal code needed to send text back to the browser.
You got /3 concepts.
Describe how to customize the status code and headers of an HttpResponse object.
Consider how to change the response details beyond just the content.
You got /2 concepts.