0
0
Djangoframework~20 mins

Process request and process response in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django Request-Response Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django view when receiving a GET request?
Consider this Django view function. What will be the HTTP response content when a GET request is made to this view?
Django
from django.http import HttpResponse

def my_view(request):
    if request.method == 'GET':
        return HttpResponse('Hello, GET request!')
    else:
        return HttpResponse('Not a GET request')
AHello, GET request!
BNot a GET request
CRaises an error because request.method is undefined
DEmpty response with status 200
Attempts:
2 left
💡 Hint
Check the request.method attribute for GET requests.
state_output
intermediate
2:00remaining
What is the value of response.status_code after this view runs?
Given this Django view, what will be the HTTP status code of the response object returned?
Django
from django.http import HttpResponse

def status_view(request):
    response = HttpResponse('OK')
    response.status_code = 201
    return response
A200
B201
C404
D500
Attempts:
2 left
💡 Hint
Look at how the status_code attribute is set on the response.
📝 Syntax
advanced
2:00remaining
Which option correctly accesses POST data in a Django view?
You want to get the value of the 'username' field sent via POST in a Django view. Which code snippet correctly retrieves it?
Django
def post_view(request):
    # retrieve username from POST data
    pass
Ausername = request.data['username']
Busername = request.GET['username']
Cusername = request.body['username']
Dusername = request.POST['username']
Attempts:
2 left
💡 Hint
POST data is accessed via request.POST dictionary.
🔧 Debug
advanced
2:00remaining
What error does this Django view raise when called?
Analyze this Django view code. What error will it raise when a request is made?
Django
from django.http import HttpResponse

def error_view(request):
    return HttpResponse('Hello') + ' World'
ATypeError: unsupported operand type(s) for +: 'HttpResponse' and 'str'
BSyntaxError: invalid syntax
CNameError: name 'HttpResponse' is not defined
DNo error, returns 'Hello World'
Attempts:
2 left
💡 Hint
Check the types involved in the + operation.
🧠 Conceptual
expert
3:00remaining
Which option best describes how Django processes a request and returns a response?
Select the option that correctly describes the sequence Django follows when processing an HTTP request and returning a response.
ADjango receives request → URL dispatcher selects view → Middleware processes request → View returns HttpResponse → Response sent to client
BDjango receives request → View processes request → Middleware processes response → URL dispatcher selects view → Response sent to client
CDjango receives request → Middleware processes request → URL dispatcher selects view → View returns HttpResponse → Middleware processes response → Response sent to client
DDjango receives request → Middleware processes response → URL dispatcher selects view → View returns HttpResponse → Response sent to client
Attempts:
2 left
💡 Hint
Think about the order of middleware and URL routing in Django's request cycle.