Christian LSANGOLA
Christian LSANGOLA

Reputation: 3317

Cannot access my Django project from LAN

Locally I can access the python project using 127.0.0.1:8000.
I want to access it via LAN as well. I've done some research how to figure it out and I've followed these steps:

in the settings.py file I did ALLOWED_HOSTS=[192.168.1.11:8000], that's my computer's LAN IP address.
Then in the terminal I've done python manage.py runserver 192.168.1.11:8000, it didn't work and I've tried to replace the IP address with 0.0.0.0:8000 but the same issue occured.

With DEBUG=True I get the following error message:

Invalid HTTP_HOST header:'<my_ip_adress>'. 
You may need to add '<my_ip_adress>' to ALLOWED_HOSTS, 

and I've already done this...

Upvotes: 0

Views: 2135

Answers (4)

dcfg
dcfg

Reputation: 891

This works for me on Ubuntu 19.10

sudo ufw allow from 192.168.1.0/24 to any port 8000 proto tcp

I've also have to enable packet forwarding in /etc/sysctl.conf and delete the # prepended to net.ipv4.ip_forward=1. Finally, I've cleared the browser cache.

Previously I was on Mint 19.03 and I only had to add the ufw rule.

Upvotes: 0

Ajay Kumar
Ajay Kumar

Reputation: 1655

In your terminal/command promt type

ifconfig #for linux

ipconfig #for windows

You will get the information about the network connection and you will find your IP. Take that IP and run as

python manage.py runserver <your-ip>

in django settings.py

ALLOWED_HOSTS = ['*']

Now you can connect this server throughout any device connected to the same network.

Upvotes: 0

John Moutafis
John Moutafis

Reputation: 23144

You need to configure your ALLOWED_HOSTS variable correct:

  • It must be a list of strings, representing the allowed hosts without defining the ports.
  • It must include every and any possible host that you want to allow.

So you need to:

ALLOWED_HOSTS = [
    '192.168.1.11',
    '127.0.0.1', # This is the one you need to allow in your case
]

Finally as @cezar points out, you need to run the server at 0.0.0.0:8000 in order to "broadcast" it on your LAN:

python manage.py runserver 0.0.0.0:8000

Good luck :)

Upvotes: 3

cezar
cezar

Reputation: 12032

If you just want to make it accessible for test purposes in the Local Area Network, then run:

python manage.py runserver 0.0.0.0:8000

In your settings.py an empty list for allowed hosts should be enough:

ALLOWED_HOSTS = []

Upvotes: 3

Related Questions