external network for django Webpage - python

I have a project in Django and it is running on my local machine, I got to make the setting by putting: Allowedhosts = ["*"] and python manage.py runserver 0.0.0.0:8000 This is enough so that I can access the page through another machine on the same network, but I would like to know a way to make access available through an external network, the question is, how do I do it ??

You need to expose your machine to the external network you are trying to access from through your router.
Be aware that with this code you will be exposing the 8000 port and everyone in that network could be able to access your site. To limit who can access, you can add the incoming IP to the ALLOWED_HOSTS variable like this:
ALLOWED_HOSTS = ['localhost', '127.0.0.1', 'EXTERNAL_IP']
Check reference at: https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts

Related

How do I make Django Website public on my local network?

I am building a website for myself and family in Django and React. Every time I ask them to check my process and get their opinions they get a webpage not found error or this site can't be reached. How do I make is so that anyone on my local network can check my page when its running. This is the default in flask, I don't understand why django runs like this? (safety maybe?) I can't find any good information on how to add an allow all to the ALLOWED_HOSTS section in the settings of the main app.
I also know that you can add IP address, But I'm a noob and I don't follow how to get the ip address of all my families devices without alot of work?? Does anyone have a good solution for this??
Django documentation:
When DEBUG is True and ALLOWED_HOSTS is empty, the host is validated
against ['.localhost', '127.0.0.1', '[::1]'].
If you are in debug mode with an empty list, or in non-debug mode also with an empty list, no one in your LAN network could be accessed to your website.
In this case, because you are in a LAN, you could use:
ALLOWED_HOSTS = ['*']
Then run
python manage.py runserver 0.0.0.0:8000
You don't need to open any ports on your router if you are in LAN, but maybe you need to configure your firewall in your local machine.
Otherwise, a web server like nginx or apache is recommended.
It is very simple
just go to your command line and type ipconfig if its windows and ifconfig if it is linux
Note your ip address on your local network
Write that address in allowed hosts in settings.py
Run command python manage.py runserver :
Take any device connect to your local network and in its browser window type servers ip:port number
For eg 192.168.25.1:8080
If all are on the same network then run
python manage.py runserver 0.0.0.0:8000
ALLOWED_HOSTS = ['*']
But this is not your real problem. You'll most likely need to open ports in the firewall of either your local machine or your router (or both) before other machines on the network will be able to reach yours.

Cannot access Django server on EC2 instance

I created an EC2 instance. Installed Python 3.4 on it and then installed Django 1.10.6 on it. I was trying to develop my first django application
I started django server.
python manage.py runserver
I could not access at http://n.n.n.n:8000.
I get a ERR_CONNECTION_REFUSED error.
I went back to the EC2 instance and added the protocol/port to the security group. This is how it looks after I add the port/proptocol
Custom TCP Rule TCP 8000 0.0.0.0/0
Custom TCP Rule TCP 8000 ::/0
It did not work. I even added a rule to allow all traffic from anywhere. It still did not work.
However, if I start django server the following way
python manage.py runserver 0.0.0.0:8000
I get the following error:
DisallowedHost at /
Invalid HTTP_HOST header: 'n.n.n.n:8000'. You may need to add 'n.n.n.n' to ALLOWED_HOSTS.
If tried adding the IP to ProjectName/settings.py,
ALLOWED_HOSTS = ['n.n.n.n'] #Make sure your host IP is a string
I get the ERR_CONNECTION_REFUSED error
I can ping the IP. I can ssh (there is rule to allow ssh). Does not look like there is a firewall.
$ sudo service iptables status
iptables: Firewall is not running.
Why am I not able access http/django server?
Thanks
Actually, I must not have started the server right, when I added the host to the ProjectName/settings.py file. I tried again, and this time it worked.
So looks like
django server started on EC2 instances with the following
python manage.py runserver
may not be accessible from other machines.
django server started with the following
python manage.py runserver 0.0.0.0:8000
would be accessible from the Internet provided the IP address of the host machine is added to the ALLOWED_HOSTS in the ProjectName/settings.py file
ALLOWED_HOSTS = ['n.n.n.n'] #Make sure your host IP is a string
This is all in addition to the entries in the security group
The IP to the allowed hosts means the IP of the devices which you want to access Django from.

How to access a server running on a local network

I'm running a Django based server on a Mac. When running it using the default "python manage.py runserver" I can access it through a browser using "http://127.0.0.1:8000/".
The thing is I want to access it from other machines on the network. When I go to network settings I can see that the IP is for example 10.0.0.15. In the past I used to be able to run a Django based server using "python manage.py runserver 10.0.0.15:8000" and then access it using that address from any other machine in the local network, including the machine running the server.
Now, when I'm trying to access it using "http://10.0.0.15:8000/" even from a browser on the same machine I can see in the terminal that the server has received a request, but I get a Bad Request (400) response. What am I doing wrong?
I tried running python manage.py runserver 0.0.0.0:8000 as Leonardo Andrade suggested. It still didn't work, but it was the first part of the solution. The problem was that the DEBUG setting was False. In that case Django takes into account the ALLOWED_HOSTS setting.
Possible solutions:
Set DEBUG setting to True. This is a good idea for debugging anyway. ALLOWED_HOSTS setting is not taken into account in that case.
Keep DEBUG setting as False and add to ALLOWED_HOSTS '0.0.0.0' or '*' or your local ip (e.g. '10.0.0.15'). This is only for testing purposes. Make sure you don't allow just any hosts when you run a production server.
Of course, run the server on 0.0.0.0:8000.
A nice solution for accessing the server from other machines when the server runs an OS X is to go to Settings->Sharing on your Mac. There, under Computer Name you can see the name other computers can use to access this computer in a local network (e.g. my-mac.local). You can add that name to ALLOWED_HOSTS and then access the server using it (e.g. http://my-mac.local:8000/).
Try to run python manage.py runserver 0.0.0.0:8000

Accessing django project in LAN systems

I used django and developed a site which is working fine, and its about to move to production and ready for deployment in a couple of weeks.
So before moving to production, i want to share the site with some of my employees to check the functionality and something else. Actually their systems are connected in LAN with mine.
So my system IP address is something like 192.168.12.135, when we run run django development server its runs at localhost:8000, i mean with the system IP address and with a port 8000 like 192.168.12.135:8000 right.
So i had shared them the project site link as 192.168.12.135:8000, but when they tried on the systems which are connected in LAN, it is not accessible and displaying an error Server not found.
I tried the above same way because recently i used python web.py framework and developed a minimal site , and when we run the server, it by default runs as localhost:8080 , and when i accessed this link from others system that are connected in LAN with mine as 192.168.12.135:8000 , its working fine and is accessible.
So can anyone please let me know
1. How to access the site on the systems that are connected in LAN before moving to production(in some real servers like apache, nginx etc.,).
2. Basically i am new to web developing and this is my first site developed in python, so
i don't know more about servers and deploying a project. So can anyone please let me know
the detailed information about deploying django on different servers
(First of all i am looking for a solution for 1st problem(Accessing in LAN before moving to
production))
If you run
python manage.py runserver 0.0.0.0:8000
your development server will be available on port 8000 to anyone on your LAN and on localhost as well (and it does not depend on your ip address)
You need to explicitly tell the development server to run on your IP rather than localhost.
Try python manage.py runserver your_ip:port.
Though it'll be accessible if you're running through apache or any other webservers other than the development server.
And to your 1st question, I would advice you to host and use a local apache server rather than using development server. Doing so, you can foresee the issues you'll be facing when moving to production.
And to 2nd, there are plenty of resources available configuring Django with different servers. Hail Google. :)
In your settings.py change ALLOWED_HOSTS to
ALLOWED_HOSTS = ['*']
Run your server by entering the following command
python manage.py runserver 0.0.0.0:8000
In order to access the project from another device enter the IP address of the server followed by the port number, which is 8000 in this example.
On windows I did everything you said but one thing was missing at my end to connect through Wi-Fi..
In settings.py:
ALLOWED_HOST = ['*']
Put Network profil in Private mode:
Windows > Settings > Network & Internet > Wi-Fi > (Click on_your_network) > In Network profil select: Private
Exemple: Run your server on the port 8000:
python manage.py runserver 0.0.0.0:8000
Then to access to the server with your other devices connected to the same network, enter the IPv4's server address with the your port (here 8000)
Exemple, if the IPv4's server address is 192.168.20.26 put the folling text directly in your browser:
192.168.20.26:8000

Access from external to python development server

I can't access externally to python development server, I have a very small django project running on my machine, and now I want to enable computers in the same LAN have access to it, but it can't do.
There is no firewall running on my machine. Is there a way around this?
How are you running the server?
Have you tried something like this?
manage.py runserver 0.0.0.0:8080
From the documentation:
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.
0.0.0.0 means: bind to all IP addresses this computer supports. So, as TheSingularity says, you'll then be able to access your Django app by entering the private IP address usually beginning with 192.168.*; which is not accessible from the Internet.
run your django app like this:
./manage.py runserver 0.0.0.0:8800
you can access now your project from other machine like this:
http://<ip_address_machine_where_project>:8800

Categories