Handling different HTTP methods lets your web app respond correctly to actions like viewing, sending, or updating data.
When you want to show a form on a page and also process the form submission.
When your app needs to respond differently to a page visit (GET) versus a form submission (POST).
When building APIs that accept data updates (PUT) or deletions (DELETE).
Syntax
Django
from django.http import HttpResponse
from django.views.decorators.http import require_http_methods
@require_http_methods(["GET", "POST"])
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")
Use request.method to check which HTTP method was used.
The decorator @require_http_methods limits allowed methods and returns 405 error if others are used.
Examples
Basic example checking GET and POST inside one view function.
Django
from django.http import HttpResponse
def example_view(request):
if request.method == "GET":
return HttpResponse("Show data")
elif request.method == "POST":
return HttpResponse("Save data")
Using a decorator to allow only GET requests.
Django
from django.http import HttpResponse
from django.views.decorators.http import require_GET
@require_GET
def only_get_view(request):
return HttpResponse("Only GET allowed")
Using a decorator to allow only POST requests.
Django
from django.http import HttpResponse
from django.views.decorators.http import require_POST
@require_POST
def only_post_view(request):
return HttpResponse("Only POST allowed")
Sample Program
This Django view responds differently based on whether the user sends a GET or POST request.
Django
from django.http import HttpResponse
from django.views.decorators.http import require_http_methods
@require_http_methods(["GET", "POST"])
def greet(request):
if request.method == "GET":
return HttpResponse("Hello! You sent a GET request.")
elif request.method == "POST":
return HttpResponse("Hello! You sent a POST request.")
OutputSuccess
Important Notes
Always handle unexpected methods to avoid errors.
Use decorators to keep your code clean and restrict methods easily.
GET requests should not change data; POST is for sending data.
Summary
Check request.method to handle different HTTP methods in one view.
Use decorators like @require_http_methods to restrict allowed methods.
Respond differently to GET and POST to support forms and data updates.
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: