def cors_enabled(origin, methods=['GET']):
 def decorator(f):
  @wraps(f)
  def decorated_func(request, *args, **kwargs):
   if (request.method == 'OPTIONS'):
    if (('HTTP_ACCESS_CONTROL_REQUEST_METHOD' in request.META) and ('HTTP_ACCESS_CONTROL_REQUEST_HEADERS' in request.META)):
     response = http.HttpResponse()
     response['Access-Control-Allow-Methods'] = ', '.join(methods)
     response['Access-Control-Allow-Headers'] = request.META['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']
    else:
     return http.HttpResponseBadRequest()
   elif (request.method in methods):
    response = f(request, *args, **kwargs)
   else:
    return http.HttpResponseBadRequest()
   response['Access-Control-Allow-Origin'] = origin
   return response
  return decorated_func
 return decorator

def cors_enabled ( origin , methods = [ 'GET' ] ) : def decorator ( f ) : @ wraps ( f ) def decorated_func ( request , * args , ** kwargs ) : if ( request . method == 'OPTIONS' ) : if ( ( 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' in request . META ) and ( 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS' in request . META ) ) : response = http . HttpResponse ( ) response [ 'Access-Control-Allow-Methods' ] = ', ' . join ( methods ) response [ 'Access-Control-Allow-Headers' ] = request . META [ 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS' ] else : return http . HttpResponseBadRequest ( ) elif ( request . method in methods ) : response = f ( request , * args , ** kwargs ) else : return http . HttpResponseBadRequest ( ) response [ 'Access-Control-Allow-Origin' ] = origin return response return decorated_func return decorator
a simple decorator to enable cors .