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. Get boolean value from request send by Ajax by zalun 4 years, 9 months ago
  2. Allow separation of GET and POST implementations by agore 1 year, 10 months ago
  3. Get typed dictionary from request GET or POST prameters (MergeDict) by pahaz 1 year, 2 months ago
  4. Declaring django views like web.py views by danigm 4 years, 2 months ago
  5. Simple views method binding by Spike^ekipS 5 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?)