Reputation: 465
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?
Upvotes: 6
Views: 5007
Reputation: 335
I run onto an additional issue following this post. If you are using a Red Hat Enterprise Linux-based Linux distribution and if firewalld is enabled, it might be blocking the connection. So for testing purposes, run:
systemctl stop firewalld
Upvotes: 2
Reputation: 1262
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
Upvotes: 3
Reputation: 1
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 IP address, 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 IP address 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 computer's IP address, 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 system's firewall.
Upvotes: 0
Reputation: 4526
You will need your local IP address to be not public.
You can get the local IP address on a Windows machine by typing the following command in cmd.exe: ipconfig
.
On Linux, type the following in the 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
Upvotes: 0