0
0
Djangoframework~5 mins

HttpResponse object in Django - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AA database query result
BA request sent by the user
CA response sent back to the user
DA template file
Which module do you import HttpResponse from?
Adjango.views
Bdjango.http
Cdjango.shortcuts
Ddjango.template
What is the default status code of an HttpResponse if not specified?
A302
B500
C404
D200
How do you set a 404 status code in an HttpResponse?
AHttpResponse('Not found', status=404)
BHttpResponse('Not found', code=404)
CHttpResponse('Not found', error=404)
DHttpResponse('Not found').set_status(404)
How can you add a custom header 'X-Test' with value '123' to an HttpResponse?
Aresponse['X-Test'] = '123'
Bresponse.add_header('X-Test', '123')
Cresponse.set_header('X-Test', '123')
Dresponse.headers['X-Test'] = '123'
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.