我是一个 Django 初学者。所以我阅读了教程并遵循它们。 但我收到一个错误页面。 '问题是什么...?' (我安装的是python 3.6.5版本)
我的错误:
Using the URLconf defined in mysite.urls, Django tried these URL
patterns, in this order:
1.polls/
2.admin/
The empty path didn't match any of these.
民意调查/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
民意调查/views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
最后!! mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
但是运行python manage.py runserver
会出现此错误:
Page not found !!, The empty path didn't match any of these!!!
所以我需要你的帮助,请帮助我......!!
最佳答案
我假设您的mysite
是“Django项目名称”,因此它包含rooturls.py
。如果我们查看该文件,我们会看到:
# mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path(<b>'polls/'</b>, include('polls.urls')),
path('admin/', admin.site.urls),
]
因此,这意味着 polls/urls.py
中的所有 urlpatterns
都是此 path('polls/', .. .)
,因此您可以说 polls/urls.py
中的 URL 的“前缀”为 'polls/'
隐式地。
为了到达 index(..)
View ,我们必须找到一条从根 URL 到该特定 View 的“路径”。实现此目的的唯一方法是首先采用 'polls/'
路径,然后在 polls/urls.py
中选择 ''
路径。因此路径是 polls/
。
因此,为了“触发”此 View ,您需要查询如下 URL:
https://localhost:8000/<b>polls/</b>
(当然,如果您指定了其他端口,或者在其他服务器上运行 Django Web 服务器,则需要相应地修改 localhost
和 8000
)。
关于python - Django : Page not found (404) Why. ..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52027225/