DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Securing REST APIs With Nest.js: A Step-by-Step Guide
  • How to Secure Fintech REST APIs Against BOLA Vulnerabilities
  • GraphQL Isn’t Dead Yet, AI Agents Revived It
  • FastAPI + Django in Production: Lessons From a Hybrid Stack

Trending

  • Supercharging AI Agents with Azure Context: A Hands-On Guide to Azure MCP
  • Why AI Agents Need a Shared Enterprise Ontology
  • When RabbitMQ Accepts a Message but Does Not Route It to a Queue
  • Why We Chose Iceberg Over Delta After Evaluating Both at Scale

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. How to Fix Django CORS Error

How to Fix Django CORS Error

Fix Django Cors error using django-cors-headers

By 
Shital Kat
·
Sep. 08, 20 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
130.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

One of the common errors we get each time when we consume Django API is CORS error.
The error might say something like: Access to XMLHttpRequest at 'url’' from origin  has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Django comes with a bunch of securities. CORS (Cross-origin resource sharing) permission request is one of them.  A request for a resource outside of the origin is known as a cross-origin request. The web page from outside the domain is requesting Django to share its resources. And Django is not giving permission.

i.e. Access to Django page and resource has blocked by CORS policy.

Now, let's see how to give a permission and solve the error in Just 4 steps:

Step 1 - Install django-cors-headers

Shell
 




xxxxxxxxxx
1


 
1
pip install django-cors-headers



Step 2 - Add corsheader  to the Installed App list in settings.py 

Python
 




xxxxxxxxxx
1


 
1
INSTALLED_APPS = [
2
    ...
3
    'corsheaders',
4
    ...
5
]
6



Step 3 -  Add CorsMiddleware to middleware list in settings.py

Python
 




xxxxxxxxxx
1


 
1
MIDDLEWARE = [
2
    ...
3
    'corsheaders.middleware.CorsMiddleware',
4
    ...
5
]



Step 4 - You have two alternatives here. Either follow Option A or Option B

Step 4 ( Option A) - Allow access to all domains by just Adding the following variables in settings.py:

Python
 




xxxxxxxxxx
1


 
1
ALLOWED_HOSTS=['*']
2
CORS_ORIGIN_ALLOW_ALL = True



Step 4 ( Option B) - Do not allow access to all the domains, but the one which you are consuming the API. Add following variables in settings.py

Python
 




x


 
1
ALLOWED_HOSTS=['http://localhost:5000']
2
               
3
CORS_ORIGIN_ALLOW_ALL = False
4
CORS_ORIGIN_WHITELIST = (
5
       'http://localhost:5000',
6
)



You are good to go now. Thanks for reading

Django (web framework) Middleware REST

Opinions expressed by DZone contributors are their own.

Related

  • Securing REST APIs With Nest.js: A Step-by-Step Guide
  • How to Secure Fintech REST APIs Against BOLA Vulnerabilities
  • GraphQL Isn’t Dead Yet, AI Agents Revived It
  • FastAPI + Django in Production: Lessons From a Hybrid Stack

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook