0
0
Djangoframework~20 mins

View decorators (require_GET, require_POST) in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django View Decorator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a Django view uses @require_GET and receives a POST request?
Consider a Django view decorated with @require_GET. What is the behavior when this view receives a POST request?
Django
from django.views.decorators.http import require_GET
from django.http import HttpResponse

@require_GET
def my_view(request):
    return HttpResponse('Hello GET')
AThe view returns 'Hello GET' regardless of the request method.
BThe view processes the POST request as a GET request silently.
CThe view raises a 405 Method Not Allowed error for the POST request.
DThe view raises a 404 Not Found error for the POST request.
Attempts:
2 left
💡 Hint
Think about what HTTP status code means 'Method Not Allowed'.
📝 Syntax
intermediate
1:30remaining
Which decorator correctly restricts a Django view to POST requests only?
Select the correct decorator to restrict a Django view to accept only POST requests.
A@require_POST
B@require_GET
C@require_http_methods(['GET', 'POST'])
D@require_safe
Attempts:
2 left
💡 Hint
Look for the decorator that explicitly mentions POST.
🔧 Debug
advanced
2:30remaining
Why does this Django view raise a 404 error instead of 405 when using @require_POST?
Given the code below, why does the view return a 404 Not Found error instead of a 405 Method Not Allowed when accessed with GET?
Django
from django.views.decorators.http import require_POST
from django.http import HttpResponse

@require_POST
def my_view(request):
    return HttpResponse('Hello POST')
ADjango always returns 404 for disallowed methods by default.
BThe decorator is incorrectly imported, causing it to fail silently.
CThe view is not decorated properly; it should be @require_GET instead.
DThe URL pattern is missing, so Django returns 404 before the decorator runs.
Attempts:
2 left
💡 Hint
Think about what happens if the URL is not matched in Django.
🧠 Conceptual
advanced
2:00remaining
What is the difference between @require_GET and @require_safe decorators in Django?
Choose the option that best describes the difference between @require_GET and @require_safe decorators.
A@require_GET allows only GET requests; @require_safe allows GET, HEAD, and OPTIONS requests.
B@require_GET allows all safe methods; @require_safe allows only GET.
C@require_GET allows GET and POST; @require_safe allows only GET.
D@require_GET allows only POST; @require_safe allows GET and POST.
Attempts:
2 left
💡 Hint
HEAD requests are considered safe HTTP methods.
state_output
expert
2:30remaining
What is the HTTP status code returned by a Django view decorated with @require_http_methods(['POST', 'PUT']) when accessed with a DELETE request?
Given a Django view decorated with @require_http_methods(['POST', 'PUT']), what HTTP status code will the server respond with if the client sends a DELETE request?
Django
from django.views.decorators.http import require_http_methods
from django.http import HttpResponse

@require_http_methods(['POST', 'PUT'])
def my_view(request):
    return HttpResponse('Hello')
A200 OK
B405 Method Not Allowed
C500 Internal Server Error
D404 Not Found
Attempts:
2 left
💡 Hint
Think about what status code means the method is not allowed.