Django, React, and CORS
I started my Django app on port 8000 and my React app on port 3000. Now I have problems with CORS errors. Help!
If you’ve had this problem, you are not alone. The answer to your problem is easier than you might think.
But what about my CORS errors?
First we have to understand what the error means. CORS means Cross Origin Resource Sharing. We can ignore “Resource Sharing”, but “Cross Origin” is important. You have two origins: port 8000 and port 3000. Those are just as different to the browser as YourSite and UntrustedCorp. There are strict rules what can be shared between YourSite and UntrustedSite.
The Origin is the place where you got the HTML from. If you get your HTML from the React dev server on port 3000, you’ll get CORS errors accessing Django because it’s a different origin.
But how do I fix this?!
Serve the initial HTML from Django. That’s it.