空路径与任何这些都不匹配(django) [英] The empty path didn't match any of these (django)
问题描述
在 personal_portfolio.py
中包含 path('',include('projects.urls'))
时,该解决方案确实有效.但这是我的烦恼...
A similar question was asked here in The empty path didn't match any of these. The solution did indeed work when I included the path('',include('projects.urls'))
within personal_portfolio.py
. But here's what bugs ME...
personal_portfolio/urls.py:
personal_portfolio/urls.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('projects/', include('projects.urls')),
]
projects/urls.py:
projects/urls.py:
urlpatterns = [
path("", views.project_index, name="project_index"),
path("<int:pk>", views.project_detail, name="project_detail"),
]
包含空路径(在后者中)!调用 path('projects/',include('projects.urls'))
时,应调用 projects/urls.py
.
The empty path IS contained (in the latter)! when the path('projects/', include('projects.urls'))
is called, it should call projects/urls.py
.
然后 有空路径.为什么我需要在 personal_portfolio/urls.py
中包含 path(",include('projects.urls'))
才能起作用?!
Then the empty path is there. Why do I need to include path("",include('projects.urls'))
in personal_portfolio/urls.py
for it to work?!
推荐答案
path(",...)
包含在 path('projects/',include('projects.urls'))
,因此路径为:
path("", ...)
is included by path('projects/', include('projects.urls'))
, therefore the path is:
'projects/' + "" = "projects/"
因此,您需要访问 localhost:8000/projects/
,对 localhost:8000/
的请求将失败.
Therefore you need to access localhost:8000/projects/
and a request to localhost:8000/
will fail.
如果您使用 path(",而是使用include('projects.urls'))
,则路径为:
If you use path("", include('projects.urls'))
instead, then the path is:
"" + "" = ""
现在,对空路径 localhost:8000/
的请求将成功.
Now a request to the empty path localhost:8000/
will succeed.
这篇关于空路径与任何这些都不匹配(django)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!