Setting up a Django development server that can be accessed by other devices on my network

Question:

I want to set up a Django development server that both my computers and smart phones can access whilst on my network via wifi.

I’ve already set up a development server that my computer can access on http://127.0.0.1:8000/. However, my other devices can’t.

The Django 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 or :: (with IPv6 enabled).”

I’ve found my “public IP address” and tried to use this by:
python manage.py runserver xx.xx.xxx.x (where this is my public ip address) but i get a “Command error: ‘xx.xx.xxx.x’ is not a valid port number or address:port pair.”

I then tried the same with :8000 after the IP address, but got an error “Error: That IP address can’t be assigned to”.

Then python manage.py runserver 0.0.0.0:8000. The command line reports “Starting development server at …”, but when i try “http://0.0.0.0:8000/” on Chrome, i get a “This site can’t be reached error”.

Is it something to do with my windows firewall settings?
Please can you someone help me? Thanks!

Asked By: Mark

||

Answers:

You will need your local ip address not public.
You can get the local ip in windows machine by typing the following command in cmd : ipconfig .

On Linux type the following in terminal : ifconfig

The ip address will be of the form 192.168.0.101[Example]

So in your phone’s browser type : 192.168.0.101:8000

Answered By: Stack

0.0.0.0 is not a real address, it’s a placeholder that just says that the server is not bound to a specific IP.

If you run on 127.0.0.1, it will only answer to queries that where addressed to 127.0.0.1, so localhost only.

Using your private address (192.168.0.x most often), it will only answer to queries to this address (so opening with the 127.0.0.1 should not work, but sometime does depending on the implementation)

So, if you use 0.0.0.0, it will answer to anything.

tl;dr : use 0.0.0.0 and connect using :

  • 127.0.0.1 from this computer

  • your computer’s private ip address for other computers inside your lan

  • you public IP for computers outside your lan. Note that this will require additional configuration on your router

Answered By: Anthony Rossi

This is very late, but I run onto an additional issue following this post. If you are using a rhel distro based and if firewalld
is enabled it might be blocking the connection. So for testing purposes run:

systemctl stop firewalld
Answered By: user2171775

127.0.0.1 defaults to the localhost of the system. So connecting with that address from another device, is telling that device to connect to itself. you can use your ipaddress, but in other for django to allow it. Go to your projects settings.py file. You should see an option ALLOWED_HOSTS = []. Put in your ipaddress in quotes, e.g ‘192.168.xxx.xxx’. Save it and when next you want to run the server. add the ipaddress:8000 after issuing the command. This will allow django to host your site on your computers ipaddress, but only devices on the same wifi can access it. The scope of that is beyond the question. You might also need to allow Django to bypass your systems firewall.

Answered By: luko
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.