80

I have installed Django server and it can be accessed as below

http://localhost:8000/get-sms/
http://127.0.0.1:8000/get-sms/

suppose My IP is x.x.x.x .

From another PC under the same network when I do

my-ip:8000/get-sms/

but it is not working.

I can easily ping my IP with that computer.

Moreover, on my port 81, I have apache, which is easily accessible like below

http:///my-ip:81

What can be the issue? Do I need something extra in Django

sumit
  • 15,003
  • 12
  • 69
  • 110
  • You can either configure your apache to forward things to your localhost:8000 or bind your development server to your lan ip. – msvalkon Mar 03 '14 at 10:27
  • do you use python manage.py runserver? – luc Mar 03 '14 at 10:28
  • Does this answer your question? [How to make Django's devserver public ? Is it generally possible?](https://stackoverflow.com/questions/3328926/how-to-make-djangos-devserver-public-is-it-generally-possible) – alexdlaird May 07 '20 at 14:12
  • Does this answer your question? [How to access the local Django webserver from outside world](https://stackoverflow.com/questions/2260727/how-to-access-the-local-django-webserver-from-outside-world) – Matej J Sep 25 '20 at 15:47

4 Answers4

142

Running the Django Development Server
This is what you're looking for. To help you further, here is what you should do:

python manage.py runserver 0.0.0.0:8000

By the way, this may be a duplicate of this question.

Here is what the documentation says:

Note that the default IP address, 127.0.0.1, is not accessible from other machines on your network. To make your development server viewable to other machines on the network, use its own IP address (e.g. 192.168.2.1) or 0.0.0.0.

Faheel
  • 2,435
  • 24
  • 33
Depado
  • 4,811
  • 3
  • 41
  • 63
  • @Depado IP address 0.0.0.0 is a special address, which means default route, when used in terms of networking. Hence when I run my Django server on 0.0.0.0 the browser says "Invalid address". Moreover the `ALLOWED_HOSTS` is set to "*". Any solution? – Akshay Chordiya Aug 28 '17 at 07:32
  • 1
    You should access the server using the IP of the machine on which it runs. Accessing it locally should be done using `127.0.0.1` otherwise you use the IP of the machine it runs on. I don't know if that's clear :/ That technique basically allows external connections. – Depado Aug 28 '17 at 10:05
13

To add to @Depado 's answer you may need to add your LAN IP address to ALLOWED_HOSTS in the settings.py along with localhost. it would look like,

ALLOWED_HOSTS = ["localhost", "192.168.8.160"]

(if localhost isn't working use 127.0.0.1 as suggested by @Sabito 錆兎)

Achala Dissanayake
  • 810
  • 3
  • 16
  • 33
4

You can use https://ngrok.com/ this will expose your local web server to the internet/public.

Venkatesh Bachu
  • 2,348
  • 1
  • 18
  • 28
1

Everywhere I looked, I kept seeing the answer to use the terminal command:

python manage.py runserver 0.0.0.0:8000

That works, but not if you want to run a remote debugger across the LAN (in my case VSCode), which launches the server automatically without a chance to modify the host ip address. However, I found a permanent solution:

Open: ./env/lib/python3.8/site-packages/django/core/management/commands/runserver.py

Search for: self.addr = ''

Replace '' with '0' and save. ('0' is shorthand for '0.0.0.0')

Now if you run: python manage.py runserver it is open to the local network, outputting: Starting development server at http://0:8000/

Importantly, the debugger now launches the server at http://0:8000/

If you haven't already, remember to add your client to allowed hosts in settings.py: ALLOWED_HOSTS = ["*"]

Blockquote

Joshua S
  • 169
  • 1
  • 10