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
Over 2 million developers have joined DZone.
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
  • Optimizing High-Volume REST APIs Using Redis Caching and Spring Boot (With Load Testing Code)
  • Evolving Spring Boot APIs to an Event-Driven Mesh
  • Advanced Middleware Architecture For Secure, Auditable, and Reliable Data Exchange Across Systems

Trending

  • Improving DAG Failure Detection in Airflow Using AI Techniques
  • Optimizing High-Volume REST APIs Using Redis Caching and Spring Boot (With Load Testing Code)
  • Self-Hosted Inference Doesn’t Have to Be a Nightmare: How to Use GPUStack
  • What Nobody Tells You About Multimodal Data Pipelines for AI Training
  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 user avatar
Shital Kat
·
Sep. 08, 20 · Code Snippet
Likes (6)
Comment
Save
Tweet
Share
129.8K Views

Join the DZone community and get the full member experience.

Join For Free

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
  • Optimizing High-Volume REST APIs Using Redis Caching and Spring Boot (With Load Testing Code)
  • Evolving Spring Boot APIs to an Event-Driven Mesh
  • Advanced Middleware Architecture For Secure, Auditable, and Reliable Data Exchange Across Systems

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • 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