0
0
Djangoframework~20 mins

Handling different HTTP methods in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HTTP Methods Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when a GET request is sent?

Consider this Django view function:

from django.http import HttpResponse

def my_view(request):
    if request.method == 'POST':
        return HttpResponse('Post received')
    return HttpResponse('Hello World')

What will the response content be when a GET request is sent to this view?

Django
from django.http import HttpResponse

def my_view(request):
    if request.method == 'POST':
        return HttpResponse('Post received')
    return HttpResponse('Hello World')
AHttpResponse with empty content
BHttpResponse with content 'Post received'
CRaises an error because GET is not handled
DHttpResponse with content 'Hello World'
Attempts:
2 left
💡 Hint

Check how the view handles methods other than POST.

📝 Syntax
intermediate
2:00remaining
Which option correctly handles GET and POST methods in a Django view?

Choose the code snippet that correctly handles GET and POST requests in a Django view function.

A
def view(request):
    if request.method == 'POST':
        return HttpResponse('POST')
    else:
        return HttpResponse('GET')
B
def view(request):
    if request.method == 'GET':
        return HttpResponse('GET')
    elif request.method == 'POST':
        return HttpResponse('POST')
C
def view(request):
    if request.method == 'PUT':
        return HttpResponse('PUT')
    else:
        return HttpResponse('GET')
D
def view(request):
    if request.method == 'DELETE':
        return HttpResponse('DELETE')
    else:
        return HttpResponse('POST')
Attempts:
2 left
💡 Hint

Remember to handle both GET and POST explicitly or with else.

🔧 Debug
advanced
2:00remaining
Why does this Django view raise an error on POST?

Examine this Django view:

def my_view(request):
    if request.method == 'POST'
        return HttpResponse('Post received')
    return HttpResponse('Hello')

What is the cause of the error when a POST request is sent?

Django
def my_view(request):
    if request.method == 'POST'
        return HttpResponse('Post received')
    return HttpResponse('Hello')
AHttpResponse is not imported causing NameError
BMissing colon ':' after the if statement causes SyntaxError
Crequest.method is not a valid attribute causing AttributeError
DIndentation error in the return statement
Attempts:
2 left
💡 Hint

Check the syntax of the if statement line.

state_output
advanced
2:00remaining
What is the response content after a PUT request?

Given this Django view:

from django.http import HttpResponse

def my_view(request):
    if request.method == 'POST':
        return HttpResponse('Post received')
    elif request.method == 'GET':
        return HttpResponse('Hello World')
    else:
        return HttpResponse('Other method')

What will be the response content if a PUT request is sent?

Django
from django.http import HttpResponse

def my_view(request):
    if request.method == 'POST':
        return HttpResponse('Post received')
    elif request.method == 'GET':
        return HttpResponse('Hello World')
    else:
        return HttpResponse('Other method')
ARaises an error because PUT is not handled
BHttpResponse with content 'Hello World'
CHttpResponse with content 'Other method'
DHttpResponse with content 'Post received'
Attempts:
2 left
💡 Hint

Check how the else block handles methods other than GET and POST.

🧠 Conceptual
expert
2:00remaining
Which HTTP method is NOT handled by this Django view and what happens?

Analyze this Django view function:

def my_view(request):
    if request.method == 'GET':
        return HttpResponse('GET method')
    elif request.method == 'POST':
        return HttpResponse('POST method')

Which HTTP method is not handled and what will the server respond with if that method is used?

Django
def my_view(request):
    if request.method == 'GET':
        return HttpResponse('GET method')
    elif request.method == 'POST':
        return HttpResponse('POST method')
AAny method other than GET or POST is not handled; server returns 500 Internal Server Error
BDELETE method is not handled; server returns 404 Not Found
CPUT method is not handled; server returns 200 OK with empty response
DPUT method is not handled; server returns 500 Internal Server Error
Attempts:
2 left
💡 Hint

Consider what happens if no return statement runs in a Django view.