HTTP method_required Decorator

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
def method_required(method):
    """
    Decorator factory that limits a view to a particular HTTP method. 
    """
    from django.http import HttpResponseNotAllowed
    def _decorator(func):
        def _closure(request, *args, **kwargs):
            if not request.method==method:
                return HttpResponseNotAllowed([method])
            else:
                return func(request, *args, **kwargs)
        _closure.__name__ = func.__name__
        _closure.__doc___ = func.__doc__
        _closure.__module___ = func.__module__
        return _closure
    return _decorator

Comments

ubernostrum (on March 29, 2008):

Django already has decorators that do this... see here for some notes.

#

(Forgotten your password?)

You may use Markdown syntax here, but raw HTML will be removed.