prevent GET or POST requests

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
from django.http import HttpResponseNotAllowed

def my_view(request):
    if request.method != 'POST':
        return HttpResponseNotAllowed('Only POST here')


# decorators
def post_only(func):
    def decorated(request, *args, **kwargs):
        if request.method != 'POST':
            return HttpResponseNotAllowed('Only POST here')
        return func(request, *args, **kwargs)
    return decorated

def get_only(func):
    def decorated(request, *args, **kwargs):
        if request.method != 'GET':
            return HttpResponseNotAllowed('Only GET here')
        return func(request, *args, **kwargs)
    return decorated
    

More like this

  1. HTTP method_required Decorator by AdamKG 5 years, 1 month ago
  2. Allow separation of GET and POST implementations by agore 11 months, 1 week ago
  3. Auto rendering decorator with options by Batiste 5 years, 3 months ago
  4. View decorator providing confirmation dialog by rudyryk 3 years, 3 months ago
  5. View Permission Decorator Helper by jgeewax 4 years, 10 months ago

Comments

jerzyk (on December 11, 2007):

django has it's own decorators from django.views.decorators.http import require_http_methods, require_GET, require_POST

#

(Forgotten your password?)