Thursday, July 16, 2015

How to access your Django site from a different computer

Hey guys! Today we'll see how to access your Django site from another computer. Now this won't be a problem if you've hosted on a remote server like PythonAnywhere or Heroku. But what if you have hosted it on your local machine and want to provide people with access to it?

I'm developing an online mentorship platform for my college that is hosted on the college server. So for that I had to use this concept. Before you do this, however, here's a disclaimer: only computers connected in the same network as your local machine can access your Django site. You can, of course, provide global access, but there's a lot of complicated networking stuff involved in that. Let's not go into that right now.

So let's get on with it. You know the command for hosting and accessing the site on your local machine is python manage.py runserver. This essentially gives your local machine server capabilities and lets you access it from the same machine. However, to let other machines access it, here's the command:
python manage.py runserver <ip_address>:<port_number>

Replace <ip_address> with your machine's IP address. If you don't know the IP address, do one of the following:
  • Windows: Open command prompt and type ipconfig. There'll be a field that reads IPv4 Address. That's the address you should put in your runserver command.
  • Linux/Mac: Open terminal and type ifconfig. There'll be a field that reads inet address. Now there might be two fields that read that. If you're connected by Ethernet, use the one listed under Ethernet. If you're connected by Wi-Fi, use the one listed under WLAN0
As for the <port_number>, the default port for any Django project is 8000. However, you can use any port you want with the exceptions of the ones used for say SMTP and Telnet.

Let's look at an example. If my IP address is 172.20.55.21 and I want to use the standard port, i.e. 8000, my command will look like this:
python manage.py runserver 172.20.55.21:8000

Then, any user wanting to access it, (again, from the same network, can't stress that enough) should simply type 172.20.55.21:8000 in the URL bar and he will be able to view and use the site.

2 comments:

  1. Didnt work for me.
    Mike

    ReplyDelete
    Replies
    1. Hey! First of all, thanks for reading my blog. Second, what exactly is the problem?

      Delete