menu

Questions & Answers

127.0.0.1 refused to connect after django manage.py runserver

I'm new to python and django. Yesterday I did the "python manage.py runserver" for the first time(just after django-admin startproject ..) and it showed starting development server at http://127.0.0.1:8000/. But in chrome it said "this site cant be reached".

After searching the error I realized I had to add '127.0.0.1' in the list of ALLOWED_HOST(which was an empty list at the beginning) in settings.py. This resolved the error and django congratulations page showed up.

But today again the same "this site cant be reached, 127.0.0.1 refused to connect" error.

Can someone help me out with this?

This link has urls.py code

This link has the project tree

EDIT: I also had problem with installation of django itself. While doing "pip install django" there was an error like "pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available" and many other line of errors. After reading comments in stack overflow someone suggested to do pip install from anaconda prompt and only after that I was able to install django.

Comments:
2023-01-24 00:55:11
Please add your settings.py code and your project tree, so we can take a look and start from tehre
Answers(8) :

Old but still without solution.

I had created

STATIC_ROOT = os.path.join(BASE_DIR, "static/")

for remote testing and could not run it locally.

After setting

STATIC_ROOT = ''

local server is working and reachable at 127.0.0.1:8000

I had the same issue and I used the whole url which is http://127.0.0.1:8000/ and it worked

I ran into the same issue.

Tried using runserver like this - python manage.py runserver 5000 and I was able to reach the site at http://127.0.0.1:5000/

for dev purposes you can add:

['*'] 

to allowed hosts.

The problem here is surely related to your OS, maybe you have another app that interfere with the django server, eventually you can run

python manage.py runserver 0.0.0.0:8000 and try to access to access the project from http://your-IPv4 address:8000, do not forget also to change your settings allowed hosts to ['*'] to accept all address.

Try to add this view to your project folder and point it to the '/' path

views.py

from django.http import HttpResponse

def homepage(request):
  return HttpResponse('hello world')

urls.py

from . import views 

urlpatterns = [
      path('/', views.homepage)
]
Comments:
2023-01-24 00:55:12
Hey, just to make sure ip -address is the IPv4 address? I followed your instruction and browser is still showing "ip-address" refused to connect
2023-01-24 00:55:12
okey could you please add your urls.py and the views.py that handle the '/' url
2023-01-24 00:55:12
I added urls.py and also editted the post. There is no views.py in the folder though

An new project doesn't need any extra settings in order to work with the development server (runserver command) so maybe you have some process bloquing the localhost address, or maybe an extension or something in your browser.

As your problem is with an empty project maybe recreating it could solve it. I know this is not a "solution" but try when you're recreating the project again that you don't miss nothing. I wrote this article some weeks ago, I you're new with django it could be useful.

Comments:
2023-01-24 00:55:12
Hey, can you explain what recreating it means. Like start a new project all over?
2023-01-24 00:55:12
Sorry. Yes, if you start a new project and the issue is still there that means that your problem is not with the project but with the browser or something in your SO (maybe firewall, disable it to discards firewall related issues)
2023-01-24 00:55:12
I've tried starting a new project but to no avail. How to check if something is wrong with the browser or SO? edge browser also showed the same error.
2023-01-24 00:55:12
If you started a new project, you used different browser and the issue is still there, that means that your project or your browsers probably (99.99% sure) dosen't have problem. So, must be some app or something in your SO, try disabling your firewall and antivirus

In my case with Python 3.7 in PyCharm, I solved the problem with upgrading Django version to 3.2.8. You may try this using the following path: File>Settings>Project Interpreter

i got he same problem recently, and got it to work by typing in the terminal: python manage.py runserver 0.0.0.0:8000