Complete worked example
python
from bootstrap_lab import django_setup
django_setup(__name__)
from django.http import JsonResponse
from django.test import Client
from django.urls import path
from django.views.decorators.http import require_http_methods
@require_http_methods(["GET", "POST"])
def roles(request):
return JsonResponse({"method": request.method}, status=201 if request.method == "POST" else 200)
urlpatterns = [path("roles/", roles)]
def request_method(method):
response = Client().generic(method, "/roles/")
return {"status": response.status_code, "allow": response.headers.get("Allow")}- Reasoning
- The decorator rejects methods outside the declared list with a method-not-allowed response and an Allow header. The view runs only for accepted operations and returns JSON with its explicit status.