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 2 years, 10 months ago
  2. ID in request GET or POST required decorator by markeyev 1 year, 8 months ago
  3. Simple views method binding by Spike^ekipS 3 years, 11 months ago
  4. PK->objects in view signature by AdamKG 4 years, 1 month ago
  5. Declaring django views like web.py views by danigm 2 years, 3 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?)