Skip to content

Instantly share code, notes, and snippets.

@arthuralvim
Created September 10, 2014 14:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthuralvim/9771f75e3f16ba5daafb to your computer and use it in GitHub Desktop.
Save arthuralvim/9771f75e3f16ba5daafb to your computer and use it in GitHub Desktop.
Django Rest Framework - Only ajax requests are permitted.
from rest_framework.permissions import BasePermission
class IsAjaxPermission(BasePermission):
"""
Check is request is ajax.
"""
def has_object_permission(self, request, view, obj):
return request.is_ajax()
def has_permission(self, request, view):
return request.is_ajax()
# if you want to fake it, just put in the request header 'HTTP_X_REQUESTED_WITH'='XMLHttpRequest'
# from django.http.request.py
# def is_ajax(self):
# return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment