0

I am developing an android app. For local testing I have created a django server on my laptop. My problem is I am not able to call django apis from app code. For example, if I want to call any django server api from desktop I write "localhost:8000/polls/link/2/". Now how to replace this "localhost part of url" if calling same api from mobile. And also my desktop is connected to internet by the same mobile hotspot. So basically both desktop and phone are on same network. My ifconfig command on desktop shows

And my desktop is Mac and mobile is Samsung core duo

Utkarsh Srivastav
  • 3,105
  • 7
  • 34
  • 53

7 Answers7

3

For developing you can use your device's inner IP address

in terminal: $ ifconfig | grep inet

you will get: inet addr:10.0.0.1 ...... use that ip with django runserver

$ python manage.py runserver 10.0.0.1:8000

from android use:

"http://10.0.0.1:8000/" as your url String

Rubber Duck
  • 3,673
  • 3
  • 40
  • 59
0

can you see the index.html on your phone browser?

localhostIP:8000

if you can, then all should be fine, if not, there is a configuration issue,

is your website on your local server accessible from the outside? How to access the local Django webserver from outside world

good luck

Community
  • 1
  • 1
manuelBetancurt
  • 15,428
  • 33
  • 118
  • 216
0

If I understand your question correctly, you have a Django installation on your laptop and you are able to hit a specific page/service locally using "localhost:8000/polls/link/2/" and it works fine. Now you want to access it from your mobile. Correct?

Replace the "localhost" with IP address of your laptop. For example, "10.0.0.1:8000/polls/link/2/"

You will find your IP address in System Preferences -> Network

MirekE
  • 11,515
  • 5
  • 35
  • 28
0

You have to set multiple things for access your API at your local network.

  1. Go to terminal and type ifconfig and get your inet address.

  2. Then add this inet address in allow_host of your project setting.py file.

  3. Start your server by using command ---> python manage.py runserver 0.0.0.0:8001 or any other port which is free.

I am assuming your inet address is 192.168.1.10

Then go to your browser and type

192.168.1.10:8001/your_app_name/your_url_of_view
GhostCat
  • 137,827
  • 25
  • 176
  • 248
Sajid
  • 31
  • 1
  • 5
0

Firstly run the django server by typing python manage.py runserver 0.0.0.0:8000. Then check the ip of your private network i.e wlan ip using ifconfig command then simply replace your localhost text with that ip while calling API.

But above all make sure that your phone and lappy should be in same network.

0

I would like to add on to all the answers

if you want to run django server on your phone follow the steps

step 1 -> run powershell type ipconfig for windows ( for mac type grep inet)

ipconfig

step 2 -> find your phone's ip address it will be of the form 198.168... type in the ip4address in the results you got from step 1.

you should be connected to the same wifi or lan to do so. So connect through hotspot ok!

step 3 -> now add this ip4 address to your setting.py of django project

ALLOWED_HOSTS = ['192.168.43.103']

**remember to add your ip4 address rather than this one **

step 4 -> after you found your ip4 address of phone now run your django project with some port. Say (your ip4 address):port here 198.168.43.103 is my ip4 address and 8000 is port (you can choose any port but i recommend make it upto 4 digits)

python manage.py runserver 198.168.43.103:8000

your operating system might ask permission to allowed your network to be viewed on other devices since it is safe for development purpose, allow it to access

step 5 -> open your android to which your are connected with network and open its browser and open your link to which you run the server in django. In my case it is -

198.168.43.103:8000

you can see the project voila!

for support mark this answer as useful

Shreyansh Gupta
  • 382
  • 1
  • 10
-1

Have you try this?
or checking firewall options.

manage.py runserver 0.0.0.0:8000
asf
  • 5
  • 1
  • 4