1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | from django.http import HttpResponseForbidden
class methods(object):
def __init__(self, *methods):
self.methods = methods
def __call__(self, f):
def wrapped_f(request, *args):
if request.method not in self.methods:
return HttpResponseForbidden('')
return f(request, *args)
return wrapped_f
GET = "GET"
POST = "POST"
PUT = "PUT"
DELETE = "DELETE"
HEAD = "HEAD"
OPTIONS = "OPTIONS"
TRACE = "TRACE"
CONNECT = "CONNECT"
|
More like this
- PK->objects in view signature by AdamKG 5 years, 1 month ago
- Using class methods as views by panyam 3 years, 11 months ago
- Method Caching by bryanhelmig 1 year, 10 months ago
- Resource by zvoase 4 years, 8 months ago
- View and StatefulView classes by Digitalxero 4 years, 7 months ago
Comments
from django.views.decorators import require_http_methods
#
Sorry
from django.views.decorators.http import require_http_methods
#
Nice to know my code is virtually identical. Makes me think I picked the right way to do it.
:)
#
This snippet only works with view functions. I have been doing some stuff with views that are methods of a class (think of an API that is analogous to django.contrib.admin, and we want some views to be methods of ModelApi, so they can be overridden), and this type of decorator doesn't quite work.
#