How to serve a django app on a local area network

In this post, I will show you how I automated the process of hosting a django app on a local area network.
The usual way to host a django app locally is by running “python manage.py runserver”. This just hosts the app at 127.0.0.1:8000. Now, this app cannot be accessed by others on your network.

In order for other devices to access your app, you will have to host the app with the IP address that your computer has on the network. For example, if your IP address is 192.168.1.3, your will have to host the django app at 192.168.1.3:8000. The port number 8000 can be different. To do so, you have to run the command “python manage.py runserver 192.168.1.3:8000”. Now your django app will be hosted on the local area network and can be accessed by others using the address http://192.168.1.3:8000/.





To get you IP address, you can run the command “ifconfig” on the shell and find out (or “ipconfig” for windows). And then you copy and paste the IP address in the command. 

But there is also a way in which you can make the django app accessible through all the IP addresses available on the local machine. And this is by using 0.0.0.0 in place of the IP address.

But you should add the domain (IP address) that is going to be used to the ALLOWED_HOSTS list in the settings.py file. This process can be automated with a small snippet of code.


Paste this snippet right below the ALLOWED_HOSTS line in the settings.py file. And to start the server, execute the command “python manage.py runserver 0.0.0.0:8000” (you can use any other port).

This code snippet will collect all the IP addresses available on the local machine and add them to the ALLOWED_HOSTS list. 

If you want to just host it only on a specific network, you can check out this git repository. Feel free to contribute to it.

Doing this saves time and effort. Also, this is useful if you are planning to use the django app in production.

If you have any questions or suggestions, please share it in the comments below.