📖 Scenario: You are building a simple Django web app that responds differently to GET and POST requests on the same URL.This is like a restaurant where the waiter listens to different requests: one to show the menu (GET) and one to take your order (POST).
🎯 Goal: Create a Django view function called handle_request that returns a different HTTP response depending on whether the request method is GET or POST.For GET requests, respond with the text 'This is a GET request'. For POST requests, respond with the text 'This is a POST request'.
📋 What You'll Learn
Create a Django view function named handle_request
Use the request.method attribute to check the HTTP method
Return an HttpResponse with the exact text 'This is a GET request' for GET requests
Return an HttpResponse with the exact text 'This is a POST request' for POST requests
💡 Why This Matters
🌍 Real World
Web servers often need to respond differently to GET and POST requests on the same URL, such as showing a form on GET and processing it on POST.
💼 Career
Understanding how to handle different HTTP methods is essential for backend web development and building RESTful APIs.
Progress0 / 4 steps
1
Import HttpResponse and create the view function
Import HttpResponse from django.http and create a function called handle_request that takes request as a parameter.
Django
Hint
Start by importing HttpResponse and define the function handle_request with request as its argument.
2
Check if the request method is GET
Inside the handle_request function, add an if statement to check if request.method is equal to 'GET'.
Django
Hint
Use if request.method == 'GET': to check the HTTP method.
3
Return HttpResponse for GET and POST methods
Inside the if block for GET, return HttpResponse('This is a GET request'). Add an elif block to check if request.method is 'POST' and return HttpResponse('This is a POST request').
Django
Hint
Use return HttpResponse('This is a GET request') inside the GET block and similarly for POST.
4
Add a fallback response for other HTTP methods
Add an else block at the end of the handle_request function that returns HttpResponse('Unsupported HTTP method') for any other HTTP methods.
Django
Hint
Use an else block to handle any HTTP methods other than GET and POST.
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
Step 1: Understand the request object in Django views
The request object contains details about the HTTP request, including the method used.
Step 2: Identify the correct attribute for HTTP method
The attribute request.method holds the HTTP method as a string like 'GET' or 'POST'.
Final Answer:
request.method -> Option A
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
Step 1: Recall Django's built-in decorators for HTTP methods
Django provides @require_http_methods to restrict allowed HTTP methods on views.
Step 2: Check correct syntax for the decorator
The decorator takes a list of allowed methods as strings, e.g., ["GET", "POST"].
Final Answer:
@require_http_methods(["GET", "POST"]) -> Option D
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
Step 1: Analyze the request method check
The view checks if request.method is 'GET' or 'POST' and returns corresponding responses.
Step 2: Determine response for POST request
Since the request is POST, the view returns 'This is a POST request' wrapped in an HttpResponse.
Final Answer:
This is a POST request -> Option B
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: