Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #703: added decorators to require that view be called with a gi…

…ven HTTP REQUEST_METHOD

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1016 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e7c870c36ea6f567e7834ce0e74ea0257c8631b7 1 parent c3377c1
Jacob Kaplan-Moss authored October 28, 2005

Showing 1 changed file with 28 additions and 2 deletions. Show diff stats Hide diff stats

  1. 30  django/views/decorators/http.py
30  django/views/decorators/http.py
... ...
@@ -1,9 +1,35 @@
1 1
 """
2  
-Decorator for views that supports conditional get on ETag and Last-Modified
3  
-headers.
  2
+Decorators for views based on HTTP headers.
4 3
 """
5 4
 
6 5
 from django.utils.decorators import decorator_from_middleware
7 6
 from django.middleware.http import ConditionalGetMiddleware
  7
+from django.utils.httpwrappers import HttpResponseForbidden
8 8
 
9 9
 conditional_page = decorator_from_middleware(ConditionalGetMiddleware)
  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."

0 notes on commit e7c870c

Please sign in to comment.
Something went wrong with that request. Please try again.