Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Django server on localhost

I would like to run a Django server locally using a local IP.

I have localhost mapped here:

$ head -n 1 /etc/hosts
127.0.0.1   localhost

I have this chunk of code in my settings.py:

import os
ALLOWED_HOSTS = ['HERE.IS.MY.IP', 'localhost', '127.0.0.1']
print "ALLOWED_HOSTS: {}".format(ALLOWED_HOSTS)

In my mysql database I have this site table:

mysql> select * from django_site;
+----+--------------------+----------------+
| id | domain             | name           |
+----+--------------------+----------------+
|  1 | example.com        | example.com    |
|  5 | HERE.IS.MY.IP:8000 | projectsdb     |
|  8 | 127.0.0.1:8000     | projectsdb     |
|  9 | localhost:8000     | projectsdb     |
+----+--------------------+----------------+

I run the server locally on 127.0.0.1:

$ python manage.py runserver 8000
ALLOWED_HOSTS: ['HERE.IS.MY.IP', 'localhost', '127.0.0.1']

Performing system checks...
# ...
Django version 1.10.5, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

If I go to this address http://HERE.IS.MY.IP:8000 it works. But of course I'd like to open it using a local IP such as http://localhost:8000 or http://127.0.0.1:8000. But this does not work. What am I missing?

like image 797
kaligne Avatar asked Dec 06 '17 13:12

kaligne


People also ask

Which command will you use to run Django local server?

The runserver command is a built-in subcommand of Django's manage.py file that will start up a development server for this specific Django project.

Can I host Django for free?

Developers use Heroku to deploy, manage, and scale modern apps. The platform is elegant, flexible, and easy to use, offering developers the simplest path to getting their apps to market. On Heroku, one can deploy Django apps for free. For your web app, Heroku also lets you choose your own name for your app.


1 Answers

Ok I solved it, silly me. I thought I was trying to access the server's page locally, but I wasn't. The server is running on a specific machine, and I was trying to access it locally from another machine (my laptop).

Explanations: The server runs on HERE.IS.MY.IP:8000, only locally because I only gave the port 8000:

$ python manage.py runserver 8000

I am trying to access the server from the web browser on my laptop, so the IP used is different than HERE.IS.MY.IP. If I want to access the page on http://HERE.IS.MY.IP:8000 from my laptop I have no other choice than to allow the server access to external machines by running the server like this:

$ python manage.py runserver 0.0.0.0:8000

or

$ python manage.py runserver HERE.IS.MY.IP:8000

and then to access it on my laptop's browser with the full IP (unless I map it on /etc/hosts) on http://HERE.IS.MY.IP:8000

Now if I want to access the page locally only from the server side, I have to run

$ python manage.py runserver 8000

, open a browser from the said server and then I can contact the pages http://HERE.IS.MY.IP:8000, http://127.0.0.1:8000 and http://localhost:8000 successfully.

So my mistake here was that I was accessing the server's page not from the server's browser itself but from another machine's.

like image 108
kaligne Avatar answered Sep 29 '22 06:09

kaligne