Bird
Raised Fist0
Djangoframework~20 mins

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

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

Practice

(1/5)
1. In Django, which attribute of the request object helps you determine if the HTTP method is GET or POST inside a view?
easy
A. request.method
B. request.type
C. request.http_method
D. request.action

Solution

  1. Step 1: Understand the request object in Django views

    The request object contains details about the HTTP request, including the method used.
  2. Step 2: Identify the correct attribute for HTTP method

    The attribute request.method holds the HTTP method as a string like 'GET' or 'POST'.
  3. Final Answer:

    request.method -> Option A
  4. Quick Check:

    Check HTTP method with request.method [OK]
Hint: Use request.method to check HTTP method type [OK]
Common Mistakes:
  • Using request.type instead of request.method
  • Trying to access request.http_method which doesn't exist
  • Confusing request.action with HTTP method
2. Which of the following is the correct way to restrict a Django view to only accept GET and POST requests using a decorator?
easy
A. @http_methods_only(["GET", "POST"])
B. @allow_methods("GET", "POST")
C. @accept_methods(["GET", "POST"])
D. @require_http_methods(["GET", "POST"])

Solution

  1. Step 1: Recall Django's built-in decorators for HTTP methods

    Django provides @require_http_methods to restrict allowed HTTP methods on views.
  2. Step 2: Check correct syntax for the decorator

    The decorator takes a list of allowed methods as strings, e.g., ["GET", "POST"].
  3. Final Answer:

    @require_http_methods(["GET", "POST"]) -> Option D
  4. Quick Check:

    Use @require_http_methods([...]) to restrict methods [OK]
Hint: Use @require_http_methods([...]) to limit HTTP methods [OK]
Common Mistakes:
  • Using non-existent decorators like @allow_methods
  • Passing methods as separate arguments instead of a list
  • Misspelling the decorator name
3. What will be the output of the following Django view when accessed with a POST request?
from django.http import HttpResponse

def my_view(request):
    if request.method == 'GET':
        return HttpResponse('This is a GET request')
    elif request.method == 'POST':
        return HttpResponse('This is a POST request')
medium
A. This is a GET request
B. This is a POST request
C. Error: Method not allowed
D. No response returned

Solution

  1. Step 1: Analyze the request method check

    The view checks if request.method is 'GET' or 'POST' and returns corresponding responses.
  2. Step 2: Determine response for POST request

    Since the request is POST, the view returns 'This is a POST request' wrapped in an HttpResponse.
  3. Final Answer:

    This is a POST request -> Option B
  4. Quick Check:

    POST request returns 'This is a POST request' [OK]
Hint: Match request.method to response text [OK]
Common Mistakes:
  • Assuming GET response for POST requests
  • Expecting an error without decorator
  • Missing return statement for POST branch
4. Identify the error in this Django view that tries to handle GET and POST methods:
def example_view(request):
    if request.method = 'GET':
        return HttpResponse('GET request')
    elif request.method == 'POST':
        return HttpResponse('POST request')
medium
A. Using single equals (=) instead of double equals (==) in the if condition
B. Missing import of HttpResponse
C. No return statement for other methods
D. Using elif instead of else

Solution

  1. Step 1: Check the if condition syntax

    The condition uses request.method = 'GET' which is an assignment, not a comparison.
  2. Step 2: Correct the comparison operator

    It should use == to compare values, like request.method == 'GET'.
  3. Final Answer:

    Using single equals (=) instead of double equals (==) in the if condition -> Option A
  4. Quick Check:

    Use == for comparison, not = [OK]
Hint: Use == to compare, not = [OK]
Common Mistakes:
  • Confusing assignment (=) with comparison (==)
  • Forgetting to import HttpResponse (though not asked here)
  • Assuming elif must be else
5. You want a Django view to accept only POST requests and return a 405 error for others. Which is the best way to implement this behavior?
hard
A. Check request.method inside the view and return 405 manually
B. Use @csrf_exempt decorator to allow POST only
C. Use @require_http_methods(["POST"]) decorator on the view
D. Raise Http404 if method is not POST

Solution

  1. Step 1: Understand Django's method restriction decorators

    The @require_http_methods decorator automatically restricts allowed HTTP methods and returns 405 if not allowed.
  2. Step 2: Compare manual checking vs decorator

    While manual checking is possible, the decorator is cleaner, less error-prone, and standard practice.
  3. Step 3: Evaluate other options

    @csrf_exempt is unrelated to method restriction; raising Http404 is incorrect for method errors.
  4. Final Answer:

    Use @require_http_methods(["POST"]) decorator on the view -> Option C
  5. Quick Check:

    Use @require_http_methods to restrict methods cleanly [OK]
Hint: Use @require_http_methods(["POST"]) to restrict POST only [OK]
Common Mistakes:
  • Using @csrf_exempt to control HTTP methods
  • Raising Http404 instead of 405 for wrong methods
  • Manually coding method checks instead of decorator
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