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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Securing REST APIs With Nest.js: A Step-by-Step Guide
  • Monoliths, REST, and Spring Boot Sidecars: A Real Modernization Playbook
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • Mistakes That Django Developers Make and How To Avoid Them

Trending

  • Agile’s Quarter-Century Crisis
  • Navigating and Modernizing Legacy Codebases: A Developer's Guide to AI-Assisted Code Understanding
  • The Role of AI in Identity and Access Management for Organizations
  • Navigating Change Management: A Guide for Engineers
  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
127.9K 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
  • Monoliths, REST, and Spring Boot Sidecars: A Real Modernization Playbook
  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • Mistakes That Django Developers Make and How To Avoid Them

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!