Search code examples
pythondjangoserver

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


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

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 I 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".

Has it something to do with my Windows firewall settings?


Solution

  • 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 address.

    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 it sometimes does depend 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

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