0
0
Djangoframework~10 mins

Process request and process response 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 import the Django HttpResponse class.

Django
from django.http import [1]
Drag options to blanks, or click blank then click option'
AHttpRequest
BHttpResponse
CHttpServer
DHttpClient
Attempts:
3 left
💡 Hint
Common Mistakes
Importing HttpRequest instead of HttpResponse
Using HttpServer which does not exist in Django
Confusing HttpClient with HttpResponse
2fill in blank
medium

Complete the code to define a Django view function that takes a request parameter.

Django
def my_view([1]):
    return HttpResponse('Hello World')
Drag options to blanks, or click blank then click option'
Arequest
Bresponse
Cself
Dcontext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'response' as the parameter name
Using 'self' in a function-based view
Using 'context' which is usually for templates
3fill in blank
hard

Fix the error in the code to return a plain text response with status code 404.

Django
from django.http import HttpResponse

def not_found_view(request):
    return HttpResponse('Page not found', [1]=404)
Drag options to blanks, or click blank then click option'
Acode
Bstatus
Cstatus_code
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'status' instead of 'status_code'
Using 'code' which is invalid
Using duplicate 'status_code' options
4fill in blank
hard

Fill both blanks to access a GET parameter named 'name' from the request.

Django
def greet_view(request):
    name = request.[1].get('[2]', 'Guest')
    return HttpResponse(f'Hello, {name}!')
Drag options to blanks, or click blank then click option'
AGET
BPOST
Cname
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of GET for URL parameters
Using wrong parameter name like 'user'
Forgetting to use quotes around the parameter name
5fill in blank
hard

Fill all three blanks to create a JSON response with a dictionary containing a message.

Django
from django.http import JsonResponse

def json_view(request):
    data = [1](message='Hello')
    return [2](data, safe=[3])
Drag options to blanks, or click blank then click option'
Adict
BJsonResponse
CTrue
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using list instead of dict for data
Returning HttpResponse instead of JsonResponse
Setting safe to False when sending a dictionary