| | 10 | |
|---|
| | 11 | def require_http_methods(request_method_list): |
|---|
| | 12 | """ |
|---|
| | 13 | Decorator to make a view only accept particular request methods. Usage:: |
|---|
| | 14 | |
|---|
| | 15 | @require_http_methods(["GET", "POST"]) |
|---|
| | 16 | def my_view(request): |
|---|
| | 17 | # I can assume now that only GET or POST requests make it this far |
|---|
| | 18 | # ... |
|---|
| | 19 | |
|---|
| | 20 | Note that request methods ARE case sensitive. |
|---|
| | 21 | """ |
|---|
| | 22 | def decorator(func): |
|---|
| | 23 | def inner(request, *args, **kwargs): |
|---|
| | 24 | method = request.META.get("REQUEST_METHOD", None) |
|---|
| | 25 | if method not in request_method_list: |
|---|
| | 26 | raise HttpResponseForbidden("REQUEST_METHOD '%s' not allowed" % method) |
|---|
| | 27 | return func(request, *args, **kwargs) |
|---|
| | 28 | return inner |
|---|
| | 29 | return decorator |
|---|
| | 30 | |
|---|
| | 31 | require_GET = require_http_methods(["GET"]) |
|---|
| | 32 | require_GET.__doc__ = "Decorator to require that a view only accept the GET method." |
|---|
| | 33 | |
|---|
| | 34 | require_POST = require_http_methods(["POST"]) |
|---|
| | 35 | require_POST.__doc__ = "Decorator to require that a view only accept the POST method." |
|---|