Challenge - 5 Problems
Django View Decorator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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')
Attempts:
2 left
💡 Hint
Think about what HTTP status code means 'Method Not Allowed'.
✗ Incorrect
The @require_GET decorator restricts the view to only accept GET requests. If a POST request is made, Django automatically returns a 405 Method Not Allowed response.
📝 Syntax
intermediate1: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.
Attempts:
2 left
💡 Hint
Look for the decorator that explicitly mentions POST.
✗ Incorrect
The @require_POST decorator restricts the view to POST requests only.
🔧 Debug
advanced2: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')
Attempts:
2 left
💡 Hint
Think about what happens if the URL is not matched in Django.
✗ Incorrect
If the URL pattern is missing or incorrect, Django never reaches the view or its decorators and returns a 404 error.
🧠 Conceptual
advanced2: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.Attempts:
2 left
💡 Hint
HEAD requests are considered safe HTTP methods.
✗ Incorrect
@require_GET restricts to GET requests only, while @require_safe allows GET, HEAD, and OPTIONS requests, since they are safe methods.
❓ state_output
expert2: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')
Attempts:
2 left
💡 Hint
Think about what status code means the method is not allowed.
✗ Incorrect
The decorator restricts allowed methods to POST and PUT. A DELETE request is not allowed, so Django returns 405 Method Not Allowed.