DEV Community

Nina Hwang
Nina Hwang

Posted on

CORS Explained + Enable in Python Projects


CORS("Cross-Origin Resource Sharing") refers to the situation when the domain requesting a resource is different from the domain serving that resource. This happens frequently when a front-end and a back-end are in different origins and the front-end communicates with the back-end using JavaScript code.


"Origin" is the combination of protocol(e.g. http, https), domain(e.g., localhost), and port(e.g. 80, 443, 3000, 8000).

Therefore, all these are different origins.

  • http://localhost
  • http://localhost:8000
  • https://localhost

Allow CORS

Browsers restrict cross-origin HTTP requests initiated from scripts due to security issue. Therefore, if you want to enable CORS, you should specify allowed origins(origins that are permitted to make cross-origin requests), allowed methods(HTTP methods that are allowed for cross-origin requests), and allowed headers(HTTP request headers that should be supported for cross-origin requests) etc.

Types of CORS Request

Preflight Requests

Before performing cross-domain requests, browsers will initiate "preflight" request to determine whether those requests are allowed. The preflight requests are done by OPTIONS , which is a type of HTTP methods.

Simple Requests

If the request meets certain conditions, browsers don't send preflight requests.

Simple requests satisfy these conditions:

  1. HTTP method is either:
    • GET
    • HEAD
    • POST
  2. Apart from the headers automatically set by the user agent, the only headers which are allowed to be manually set are one of these:
    • Accept
    • Accept-Language
    • Content-Language
    • Content-Type
      • Only application/x-www-form-urlencoded, multipart/form-data, text/plain are allowed as values
  3. If the request is made using an XMLHttpRequest object, no event listeners are registered on the object returned by the XMLHttpRequest.upload property used in the request
  4. No ReadableStream object is used in the request.

For more information about CORS, check this document.

CORS Hands-On


Install CORS module:
pip install django-cors-headers

ALLOWED_HOSTS = ['*'] # '*' is a wildcard which allows any host



# CORS settings


Enter fullscreen mode Exit fullscreen mode

For more information about django-cors-headers, check this document.


from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

origins = ["*"]

Enter fullscreen mode Exit fullscreen mode

For more information about FastAPI CORS setup, check this document.


Install CORS extension:
pip install -U flask-cors

from flask-cors import CORS

app = Flask(__name__)

Enter fullscreen mode Exit fullscreen mode

For more information about flask-cors, check this document.

Discussion (4)

thumbone profile image
Bernd Wechner

I appreciate the article, but you do make one mistake I see in such articles across the web. That is you quietly mix the roles and client and server in your presentation.

You start with "Most of browsers do not allow CORS" and then list three server solutions ...

It is precisely this that trips us up with CORS from the get go. What role the browser plays and what role the server plays. It's worth some effort to differentiate these clearly.

ninahwang profile image
Nina Hwang Author

Thank you for the feedback! To be honest, the concept is still not perfectly crystal clear for me, and that's why I needed to summarize it. It got a little more clearer thanks to your comment:)

ramkumarm15 profile image

Good article!!

iceorfiresite profile image
Ice or Fire

Great info!