Search code examples
djangoreactjsdjango-rest-frameworkwindows-server-2019

ReactJS API calls to Django REST - use IP or localhost?


I run a ReactJS front end application and Django REST back end/API both on the same webhost. The application works perfectly fine on localhost, however when you run it from somewhere else it can't seem to connect to the API.

Console of client's browser:

enter image description here

Django REST running on the server:

enter image description here

Am I supposed to connect to it using the external IP of the server instead of localhost? Localhost should work right, since both the frontend and Django API are hosted on the same server?


Solution

  • In case someone is looking for an answer, I've learned a few things since posting:

    • The Django API has to be accessed by the IP, not localhost.
    • Since my website is on HTTPS, Django has to be accessed by HTTPS as well or it won't work.
    • Since Django runserver doesn't support HTTPS you need to host it with a webserver (you should do this anyway in production), I used Apache for this.

    Thanks to xxnora for giving me tips and steering me in the right direction.