python 空路径与以下任何一个都不匹配:

k75qkfdt  于 2023-04-19  发布在  Python
关注(0)|答案(2)|浏览(153)

我正在使用Python 3.8,当我启动服务器时出现错误:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Using the URLconf defined in djangoProject.urls, Django tried these URL patterns, in this order:

polls/
admin/
The empty path didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

我的民意调查/urls.py:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

我的djangoProject/urls.py:

from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
    url("polls/", include('polls.urls')),
    url("admin/", admin.site.urls),
]
wz1wpwve

wz1wpwve1#

根URL包含两项:polls/admin/。这意味着如果您访问根URL(127.0.0.1:8000/),它将不会触发任何视图,因为没有视图被“附加”到该URL模式。因此,您将访问一个页面以触发视图,或者更改URL模式以在访问根URL时访问index视图。

选项一:访问/polls/

您可以通过以下方式访问该页面:

127.0.0.1:8000/polls/

选项二:将index链接到根URL

您可以使用以下命令更改URL模式,以便在访问根URL时触发index

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    #   ↓↓ empty string
    url('', include('polls.urls')),
    url('admin/', admin.site.urls),
]
dgiusagp

dgiusagp2#

我也有同样的问题,但事情非常-实际上很简单,我认为这是上面的答案-来自@Williem货车Onsem!

在mysite/url.py或任何你命名的项目,转到url.py并插入以下内容:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    #   ↓↓ empty string
    url('', include('polls.urls')),
    url('admin/', admin.site.urls),
]

相关问题