Bird
Raised Fist0
Djangoframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the @require_GET decorator in Django views?
easy
A. To allow only GET requests to access the view
B. To allow only POST requests to access the view
C. To allow both GET and POST requests to access the view
D. To disable all HTTP requests to the view

Solution

  1. Step 1: Understand the decorator's role

    The @require_GET decorator restricts the view to accept only GET requests.
  2. Step 2: Compare with other decorators

    Unlike @require_POST which allows only POST, @require_GET is specific to GET requests.
  3. Final Answer:

    To allow only GET requests to access the view -> Option A
  4. Quick Check:

    @require_GET = allows GET only [OK]
Hint: Remember GET is for reading data, require_GET allows only that [OK]
Common Mistakes:
  • Confusing @require_GET with @require_POST
  • Thinking it allows all request types
  • Assuming it disables the view
2. Which of the following is the correct way to import and use the @require_POST decorator in a Django view?
easy
A. from django.views.decorators import require_POST @require_POST def my_view(request): pass
B. from django.views.decorators.http import require_GET @require_POST def my_view(request): pass
C. from django.http import require_POST @require_POST def my_view(request): pass
D. from django.views.decorators.http import require_POST @require_POST def my_view(request): pass

Solution

  1. Step 1: Identify the correct import path

    The @require_POST decorator is imported from django.views.decorators.http.
  2. Step 2: Check usage syntax

    The decorator is applied directly above the view function with @require_POST.
  3. Final Answer:

    from django.views.decorators.http import require_POST @require_POST def my_view(request): pass -> Option D
  4. Quick Check:

    Correct import and decorator usage = from django.views.decorators.http import require_POST @require_POST def my_view(request): pass [OK]
Hint: Import from django.views.decorators.http for both decorators [OK]
Common Mistakes:
  • Importing from wrong module like django.http
  • Using @require_GET instead of @require_POST
  • Missing the decorator above the function
3. Consider this Django view code snippet:
from django.views.decorators.http import require_GET
from django.http import HttpResponse

@require_GET
def sample_view(request):
    return HttpResponse('Hello GET')

What will happen if a POST request is sent to sample_view?
medium
A. The view will return 'Hello GET' response
B. The view will raise a 405 Method Not Allowed error
C. The view will ignore the POST and treat it as GET
D. The view will crash with a server error

Solution

  1. Step 1: Understand @require_GET behavior

    The decorator allows only GET requests; other methods are blocked.
  2. Step 2: Check response to disallowed methods

    When a POST request is sent, Django returns a 405 Method Not Allowed error automatically.
  3. Final Answer:

    The view will raise a 405 Method Not Allowed error -> Option B
  4. Quick Check:

    @require_GET blocks POST with 405 error [OK]
Hint: require_GET blocks non-GET with 405 error [OK]
Common Mistakes:
  • Assuming POST is accepted and returns normal response
  • Thinking it crashes the server
  • Believing it silently converts POST to GET
4. You have this Django view:
from django.views.decorators.http import require_POST
from django.http import HttpResponse

@require_POST
def submit_data(request):
    return HttpResponse('Data submitted')

When testing, you accidentally send a GET request and get an error. What is the most likely cause?
medium
A. The @require_POST decorator blocks GET requests causing a 405 error
B. The HttpResponse is not imported correctly
C. The view is missing a return statement
D. The view function name is invalid

Solution

  1. Step 1: Analyze decorator effect on request methods

    The @require_POST decorator allows only POST requests and blocks others.
  2. Step 2: Understand error cause on GET request

    Sending a GET request triggers a 405 Method Not Allowed error because GET is disallowed.
  3. Final Answer:

    The @require_POST decorator blocks GET requests causing a 405 error -> Option A
  4. Quick Check:

    @require_POST blocks GET with 405 error [OK]
Hint: GET requests fail on @require_POST with 405 error [OK]
Common Mistakes:
  • Thinking missing return causes error
  • Assuming HttpResponse import issue
  • Blaming function name for HTTP method errors
5. You want a Django view to accept only POST requests but also want to add a custom message when a GET request is made instead of the default 405 error. Which approach correctly combines @require_POST with custom handling?
hard
A. Manually check request.method inside the view and return custom response without @require_POST
B. Use @require_POST and add a try-except block inside the view to catch MethodNotAllowed
C. Use @require_POST and catch the 405 error globally in middleware
D. Use @require_GET instead and handle POST requests manually

Solution

  1. Step 1: Understand @require_POST behavior

    The @require_POST decorator returns an HttpResponseNotAllowed (405) directly without calling the view or raising an exception.
  2. Step 2: Combine with custom handling

    To customize the 405 response while using the decorator, implement middleware that checks response.status_code == 405 in process_response and returns a custom message.
  3. Final Answer:

    Use @require_POST and catch the 405 error globally in middleware -> Option C
  4. Quick Check:

    @require_POST + middleware process_response for custom 405 [OK]
Hint: Middleware process_response customizes decorator's 405 response [OK]
Common Mistakes:
  • Manually checking method without decorator (doesn't combine)
  • Try-except in view (view not executed)
  • Using @require_GET when POST is needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard