Decorator that limits request methods

 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

  1. PK->objects in view signature by AdamKG 5 years, 1 month ago
  2. Using class methods as views by panyam 3 years, 11 months ago
  3. Method Caching by bryanhelmig 1 year, 10 months ago
  4. Resource by zvoase 4 years, 8 months ago
  5. View and StatefulView classes by Digitalxero 4 years, 7 months ago

Comments

dc (on July 25, 2009):

from django.views.decorators import require_http_methods

#

dc (on July 25, 2009):

Sorry

from django.views.decorators.http import require_http_methods

#

schinckel (on July 26, 2009):

Nice to know my code is virtually identical. Makes me think I picked the right way to do it.

:)

#

schinckel (on August 17, 2009):

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.

#

(Forgotten your password?)