2

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.

macro
  • 31
  • 1
  • 1
  • 4

9 Answers9

2

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)
]
Farhani Walid
  • 927
  • 1
  • 9
  • 20
  • 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 – macro May 20 '20 at 21:07
  • okey could you please add your urls.py and the views.py that handle the '/' url – Farhani Walid May 20 '20 at 21:09
  • I added urls.py and also editted the post. There is no views.py in the folder though – macro May 21 '20 at 04:38
1

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

vilner
  • 11
  • 1
  • 3
0

for dev purposes you can add:

['*'] 

to allowed hosts.

kaozdl
  • 1
  • 1
  • 2
0

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.

Yasiel Cabrera
  • 540
  • 4
  • 11
  • Hey, can you explain what recreating it means. Like start a new project all over? – macro May 20 '20 at 21:11
  • 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) – Yasiel Cabrera May 20 '20 at 23:05
  • 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. – macro May 21 '20 at 04:58
  • 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 – Yasiel Cabrera May 21 '20 at 13:02
0

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

0

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

0

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/

mellifluous
  • 2,345
  • 2
  • 29
  • 45
0

I got the same problem recently, and got it to work by typing in the terminal:

python manage.py runserver 0.0.0.0:8000
Sam
  • 643
  • 2
  • 13
0

I had the same issue, and I can't solve it for weeks. The solution was very easy, I realized that localhost is not reachable, if I press ctrl+break (or ctrl+c) and I stop the program.

My CMD's view:

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
July 16, 2023 - 11:46:44
Django version 4.2.2, using settings 'django_test.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

If you press CTRL-BREAK (or ctrl+c) localhost will be unreachable.