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

More like this

  1. require XMLHttpRequest view decorator by skam 4 years ago
  2. Cookieless Session Decorator by achimnol 2 years, 6 months ago
  3. LoginRequired class-based view decorator by mjumbe 6 months, 2 weeks ago
  4. Decorating class-based views by lqc 1 week, 3 days ago
  5. ID in request GET or POST required decorator by markeyev 1 year, 5 months ago

Comments

ubernostrum (on March 29, 2008):

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

#

(Forgotten your password?)