Django Cannot Be Accessed From Localhost

14,779

Solution 1

I cannot access localhost:8000 from my browser if I run python manage.py runserver

I don't know why that command cannot work in my laptop, but in my pc it just works.

====

ok, so my solution now is to use ipv6 and port 8000 which is

python manage.py runserver [::]:8000

Solution 2

Just try http://0.0.0.0:8000 instead of localhost:8000

Default localhost value is http://127.0.0.1

If you see the runerver result you have :

starting developement server at http://0.0.0.0:8000

Because you told that django server start at http://0.0.0.0:8000 when you run this command :

python manage.py runserver 0.0.0.0:8000

Solution 3

I'm also beginner to django, but I needed following things in order to run the server:

  1. In settings.py, added ALLOWED_HOSTS as
    ALLOWED_HOSTS = [ 'elearning.com' ]
    where elearning.com is my hostname. You can have multiple hosts separated with comma.
  2. Used following command to run the server:
    python manage.py runserver [::]:8000
    or
    python manage.py runserver 0.0.0.0:8000
    or
    python manage.py runserver 0:8000.
    It will now listen to all interfaces. Read Django doc here.
  3. Browsed to given hostname. In my case, it is http://elearning.com:8000/

I tried writing IP in ALLOWED_HOSTS but it didn't work, I could not open http://192.168.x.x:8000 in browser. Can anyone please correct me?

Share:
14,779
Richard Octovianus
Author by

Richard Octovianus

Updated on June 04, 2022

Comments

  • Richard Octovianus
    Richard Octovianus almost 2 years

    It's kinda weird I cannot access django from localhost but I able to access it from local IP.

    python manage.py runserver 0.0.0.0:8000
    

    then when I try to access from

    enter image description here enter image description here

    My host file

    127.0.0.1 lmlicenses.wip4.adobe.com
    127.0.0.1 lm.licenses.adobe.com
    127.0.0.1 gc.kis.scr.kaspersky-labs.com ff.kis.scr.kaspersky-labs.com ie.kis.scr.kaspersky-labs.com
    0.0.0.0 gc.kis.scr.kaspersky-labs.com ff.kis.scr.kaspersky-labs.com ie.kis.scr.kaspersky-labs.com
    

    I'm guessing something wrong with windows firewall or kaspersky,but I don't know what to do.

    I've add exception to port 8000 and python.exe too

  • Richard Octovianus
    Richard Octovianus over 8 years
    It's the same. I still cannot access even with 127.0.0.1:8000 nor 0.0.0.0:8000
  • Arash Hatami
    Arash Hatami over 8 years
    why yo want to change IP address of django server ? just run the normal command python manage.py runserver @RichardOctovianus
  • Vaibhav Jain
    Vaibhav Jain over 8 years
    Use runserver 0.0.0.0:8000 to make it listen on all interfaces from Django official documentation code.djangoproject.com/ticket/396
  • Richard Octovianus
    Richard Octovianus over 8 years
    even with just "python manage.py runserver" i cannot access through localhost:8000 in my browser. that's why i use 0.0.0.0:8000 to be able accessed from my lan ip
  • Richard Octovianus
    Richard Octovianus over 8 years
    seems nothing to do with hosts file, I tried it before
  • Vaibhav Jain
    Vaibhav Jain over 8 years
    Well in my Case it did.