Complete the code to create a simple HTTP response with the text 'Hello World'.
from django.http import HttpResponse def my_view(request): return HttpResponse([1])
The HttpResponse object expects a string as content. We must pass the text inside quotes.
Complete the code to set the HTTP status code to 404 in the response.
from django.http import HttpResponse def not_found_view(request): return HttpResponse("Page not found", status=[1])
Status code 404 means the page was not found. We set it using the status parameter.
Fix the error in the code to add a custom header 'X-Custom' with value 'Value123' to the response.
from django.http import HttpResponse def custom_header_view(request): response = HttpResponse("Content") response[[1]] = "Value123" return response
To add a custom header, use the header name as the key in the response object like response['X-Custom'].
Fill both blanks to create an HttpResponse with JSON content and set the correct content type header.
from django.http import HttpResponse import json def json_view(request): data = {"key": "value"} response = HttpResponse(json.dumps(data), content_type=[1]) response[[2]] = "application/json" return response
The content_type parameter and the 'Content-Type' header both should be set to 'application/json' to indicate JSON data.
Fill all three blanks to create an HttpResponse that redirects to '/home' with status 302 and a custom header 'X-Redirected' set to 'yes'.
from django.http import HttpResponse def redirect_view(request): response = HttpResponse(status=[1]) response[[2]] = "/home" response[[3]] = "yes" return response
Status 302 means redirect. The 'Location' header tells the browser where to go. 'X-Redirected' is a custom header.