0
0
Djangoframework~10 mins

HttpResponse object in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a simple HTTP response with the text 'Hello World'.

Django
from django.http import HttpResponse

def my_view(request):
    return HttpResponse([1])
Drag options to blanks, or click blank then click option'
AHello World
B"Hello World"
Cresponse
DHttpResponse
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the text string.
Passing a variable name instead of a string.
2fill in blank
medium

Complete the code to set the HTTP status code to 404 in the response.

Django
from django.http import HttpResponse

def not_found_view(request):
    return HttpResponse("Page not found", status=[1])
Drag options to blanks, or click blank then click option'
A404
B200
C500
D302
Attempts:
3 left
💡 Hint
Common Mistakes
Using 200 which means OK instead of 404.
Using 500 which means server error.
3fill in blank
hard

Fix the error in the code to add a custom header 'X-Custom' with value 'Value123' to the response.

Django
from django.http import HttpResponse

def custom_header_view(request):
    response = HttpResponse("Content")
    response[[1]] = "Value123"
    return response
Drag options to blanks, or click blank then click option'
A"X-Custom"
B"Set-Cookie"
C"Content-Type"
D"Authorization"
Attempts:
3 left
💡 Hint
Common Mistakes
Using standard headers like Content-Type instead of the custom header.
Forgetting to use quotes around the header name.
4fill in blank
hard

Fill both blanks to create an HttpResponse with JSON content and set the correct content type header.

Django
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
Drag options to blanks, or click blank then click option'
A"application/json"
B"text/html"
C"Content-Type"
D"Accept"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text/html' as content type for JSON data.
Setting the wrong header name like 'Accept'.
5fill in blank
hard

Fill all three blanks to create an HttpResponse that redirects to '/home' with status 302 and a custom header 'X-Redirected' set to 'yes'.

Django
from django.http import HttpResponse

def redirect_view(request):
    response = HttpResponse(status=[1])
    response[[2]] = "/home"
    response[[3]] = "yes"
    return response
Drag options to blanks, or click blank then click option'
A302
B"Location"
C"X-Redirected"
D404
Attempts:
3 left
💡 Hint
Common Mistakes
Using status 404 which means not found.
Setting wrong header names for redirect.