about

Django enable Cross Origin Resource Sharing - 05 May 2013

search

I am using a django-nonrel setup, and I needed to enable Cross-Origin Resource Sharing.

Relevant reading:

add items to HttpResponse dictionary

From https://docs.djangoproject.com/en/dev/ref/request-response/#setting-headers:

To set or remove a header in your response, treat it like a dictionary:

  response = HttpResponse()
  response['Cache-Control'] = 'no-cache'
  del response['Cache-Control']

function pastie

def addCORSHeaders(theHttpResponse):
    if theHttpResponse and isinstance(theHttpResponse, HttpResponse):
        theHttpResponse['Access-Control-Allow-Origin'] = '*'
        theHttpResponse['Access-Control-Max-Age'] = '120'
        theHttpResponse['Access-Control-Allow-Credentials'] = 'true'
        theHttpResponse['Access-Control-Allow-Methods'] = 'HEAD, GET, OPTIONS, POST, DELETE'
        theHttpResponse['Access-Control-Allow-Headers'] = 'origin, content-type, accept, x-requested-with'
    return theHttpResponse

From https://github.com/jzerbe/storagebin/blob/master/storagebin/__init__.py


131 words. Post tags: Django and CORS.

Post content is written by Jason Zerbe and licensed CC BY-NC 3.0.