Django trying to access the site results in connection-refused - django

I am trying to access my site from my phone. Both my laptop and phone connected to the same wifi network. I tried to access the website with Postman and got a successful response. However when I try to access the site from my phone I get ERR_CONNECTION_REFUSED. What can be the reason for this?
I use this test url that I know works:
http://127.0.0.1:8000/policies/myModels_asJson

127.0.0.1 is the localhost so unless you're running your django app (or at least a webserver) on your phone that's the expected result.
If you want to test from your phone, you must
make sure the django server on your laptop (I assume it's running on the laptop) listens on the laptop's own ip (which should be a local ip in the 192.168.* range) - the simplest here is to run it on 0.0.0.0 (which means "listen on all ips"): ./manage.py runserver 0.0.0.0:8000
point your phone's browser to your laptop's local ip

Related

Do we have to host our Django site for our Flutter App backend?

Suppose I am creating a todo list app in Flutter. I have used Django to connect flutter's backend with the local host database. Now my API's(Django Rest Framework) are working fine if the is on local machine. But what if I deploy my Flutter app to Play Store? Then I need to host my Django site as well or it can be upon the local machine?
Development
If you are in development the local machine is enough. For connecting the Flutter app to the Django development server follow the steps below.
connect your local server and mobile device to the same network (for example, a common wi-fi)
Get the IP address of the local machine. In Linux, the command is ifconfig,
and in Windows, it is ipconfig. It will be something like 192.168.1.8
Run the Django server with the following command in the terminal, providing your IP address you got in step 2:
python manage.py runserver 192.168.1.8:8000
To test, try access the IP address (with port) via. a browser on the phone. If it loads, the connection is correctly set up
Now you can use this address in your request in the HTTP request in Flutter
Production
If you are deploying the app in production, you can host a server (AWS/EC2, for example). You will get the IP address somewhere in the console. Deploy the code to the server and run the code there. Replace the local IP address in app with the IP address of the server. You can also use packages like Environ for this.

Configure my local flask server to be available to other devices under same wifi network

From most of the resources, I gathered-
If I declare 0.0.0.0 as host and 5000 as port explicitly,other devices under my wifi network should access it, but when I type http://0.0.0.0:5000/ from another machine(my windows laptop), it didn't work.
port 5000 needs to be open. If not then I should open that.
That didn't work for me either.
Perhaps debug=True parameter should be removed from Flask's run() method as dev server shouldn't be publicly available. Still no luck!
What else could be the problem as the following isn't working for me-
app.run(host='0.0.0.0', port=5000)
I know, this question has been asked couple of times, but none of the suggestions worked for me. So, please help me access my flask Restful API through other devices as I want to test the service having a web application and mobile application as clients.
The Flask dev server should not be used in production as it is not designed to be efficient and secure. Flask Deployment
To make the flask dev server visible on LAN/WLAN add the host parameter to run on machine's IP Address
app.run(host='0.0.0.0', port=5000)
To Access the web server use private ip address(LAN/WLAN) of the machine running the flask server 192.168.1.23:5000

Django go live with Nginx from server on local network

I followed this tutorial to test going live with a django website.
I don't have a DigitalOcean droplet but a local machine I have that eventually could be used as a server. Therefore, in the steps where I had to put the domain name I used the local machine IP address (it's the same IP address I use to login with ssh).
All steps were ok and I have a response from whitin the local machine, i.e., if I open a browser in the local machine and with the IP address, the Django app runs ok.
However, the same does not happen if I open it from an external computer. I'm not sure if it was suppose to work like that. Is it? If not what extra steps do I need to do to go public?

Django: external access

I have been setting up a Django website using the development server, and it works fine if I access it using the following address in my browser:
localhost:8000
I now want to be able to access the website from another computer (on the same network). I still want to use the development server for now. If my computer's name on the network is myname, and the domain is mydomain.com, then how can I enable this? If I just type in:
myname.mydomain.com:8000
I get a Server not found error.
Thanks.
It's not possible to access your computer like they way you explained in a LAN. There are couple of ways you can let other computers use your dev server directly.
First, run your Django app like this -
$ ./manage.py runserver 0.0.0.0:8000
So that the server listens to all the network interfaces for anyone accessing your IP, not just localhost. Then -
Find out your IP in the network. In the other computer in the same network open the hosts file(In linux, it's at /etc/hosts) and enter a new line -
enter.your.ip.here intended.domainname.com
Then you can access your dev server from that computer by entering intended.domainname.com:8000 or enter.your.ip.here:8000 in the browser. Cons: You have to alter the hosts file in each of the computer you intend to use the domain name instead of IP from and they all have to be in the same network.
You could use localtunnel. Then you can just execute -
$ localtunnel 8000
And it'll give you an url like http://xyz.localtunnel.com which you can share to anybody using the Internet and they'll be able to use your dev server.

Facebook Debugger unable to test localhost

I am building a Facebook app using Django. So, for development, I connected the app to localhost. My app is loading on canvas and working fine but the Facebook debugger is unable to test it correctly when I give localhost address as input.
These are the requests I tried in debugger
http://localhost
https://localhost/
http://127.0.0.1/
localhost
etc
Almost for all possible combinations.. It showed me
Error Parsing URL: Error parsing input URL, no data was scraped.
When I deployed the same code on heroku and tried.. It was working!
So,
Can't I debug the project on localhost? What's the point in working on it then??
If I can work, how should I fix it?
Can't I debug the project on localhost? What's the point in working on it then??
You can debug your code etc. on localhost – but of course you can’t have Facebook’s debug tool reach a site on your localhost, because Facebook (and everyone else on the web) does have no idea what machine your localhost actually is. (Absolute bascis, dude!)
If I can work, how should I fix it?
You have to make your web server accessible from the “outside”, over the internet.
Set up your test server so that it accepts requests from outside IPs, and get a DynDNS address (basically something that can be resolved by third parties like Facebook over the DNS).
You can access Facebook apps locally but you need to fake the domain of your local computer. You can do this by adding
127.0.0.1 mysite.test.example.com
to /etc/hosts. You should update mysite.test.example.com to your domain. Your Facebook app needs to be configured for that domain. You can then use the Facebook app locally and debug your project.
The alternative is to setup up a web server and use its domain for testing purposes (but this is not ideal because you'll need to commit and build the code before you can see your changes).