Login

prevent GET or POST requests

Author:
jerzyk
Posted:
December 11, 2007
Language:
Python
Version:
.96
Score:
-2 (after 6 ratings)

This will return HTTP 405 if request was not POSTed. same way you can forbide POST request, change 'POST' to 'GET'

Decorators provided for your convenience.

 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. FileField having auto upload_to path by junaidmgithub 1 month ago
  2. LazyPrimaryKeyRelatedField by LLyaudet 1 month, 1 week ago
  3. CacheInDictManager by LLyaudet 1 month, 1 week ago
  4. MYSQL Full Text Expression by Bidaya0 1 month, 1 week ago
  5. Custom model manager chaining (Python 3 re-write) by Spotted1270 1 month, 2 weeks 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

#

Please login first before commenting.