Can't access my laptop's localhost through

2019-08-04 12:22发布

问题:

So I did a research before posting this and the solutions I found didn't work, more precisely:

-Connecting to my laptop's IPv4 192.168.XXX.XXX- didnt work

-Conecting to 10.0.2.2 (plus the port) - didnt work

I need to test an API i built using Django Rest framework so I can get the json it returns, but i cant access through an Android app i'm building (im testing with a real device, not an emulator). Internet permissions are set on Manifest and i can access remote websites normally. Just can't reach my laptop's localhost(they are in the same network)

I'm pretty new to Android and Python and Django as well (used to built Django's Rest Framework API).

EDIT: I use localhost:8000/snippets.json or smth like this to connect on my laptop.

PS: I read something about XAMP server... do I need it in this case?

Thanks in advance

回答1:

Have you started the server like this?

python manage.py runserver 0.0.0.0:8000

Then, try to connect to 192.168.XXX.XXX:8000



回答2:

I tried the above, but failed to work in my case. Then with running python manage.py runserver 0.0.0.0:8000 I also had to add my IP to ALLOWED_HOSTS in settings.py, which solved this issue.

eg. Add your ip to allowed hosts in settings.py ALLOWED_HOSTS = ['192.168.XXX.XXX']

Then run the server python manage.py runserver 0.0.0.0:8000