问题
I am creating my first Django project. I have successfully installed Django version 2.1. When I created the project, the project was successfully launched at the url 127.0.0.1:8000. Then I ran the command python manage.py startapp products. Products folder was also successfully created in the project. Then inside the products folder, I opened: views.py
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
return HttpResponse('Hello World')
then products/urls:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index)
]
Inside the main project folder, I opened urls.py and I modified the code:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('products/', include('products.urls'))
]
Then the url http://127.0.0.1:8000/products/ was not opening on browser. Browser is giving the "Unable to connect" error. I am using the PyCharm IDE. I will really appreciate your answer.
回答1:
In you settings.py add the app to the INSTALLED_APPS list as:
INSTALLED_APPS =
[
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'products' # <-- your product app
]
And modify ALLOWED_HOSTS list as:
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
Then run the application:
python manage.py runserver 127.0.0.1:8080
.
回答2:
Check if you had ran the command below:
python manage.py runserver
If you had run the above command and the error persists, try to run on the global address 0.0.0.0
python manage.py runserver 0.0.0.0:8000
OR on a different port
python manage.py runserver 0.0.0.0:8001
来源:https://stackoverflow.com/questions/59598620/django-error-browser-is-unable-to-connect-at-127-0-0-18000-products