如何修复 "Page not found (404)" 错误 ("Django tried these URL patterns... The empty path didn't match any of these.")

How to fix "Page not found (404)" error ("Django tried these URL patterns... The empty path didn't match any of these.")

作为Django的新手,我遇到了和之前很多人一样的问题。如果您没有立即将我的问题标记为双重问题,我将不胜感激,因为我检查了那些旧帖子建议的修复但无济于事。

我正在关注 this tutorial and have finished with all up to the heading "Projects App: Templates". Now when I start the server, at http://localhost:8000/ 我得到:

Page not found (404) Request Method: GET Request URL: http://localhost:8000/

Using the URLconf defined in personal_portfolio.urls, Django tried these URL patterns, in this order:

admin/
projects/

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.

这是我 运行 服务器时的控制台输出:

System check identified no issues (0 silenced).

April 05, 2019 - 15:31:54

Django version 2.2, using settings 'personal_portfolio.settings'

Starting development server at http://127.0.0.1:8000/

Quit the server with CTRL-BREAK.

Not Found: /

[05/Apr/2019 15:32:01] "GET / HTTP/1.1" 404 2042

Not Found: /favicon.ico

[05/Apr/2019 15:32:01] "GET /favicon.ico HTTP/1.1" 404 2093

我尝试过但没有帮助的方法:

  1. 正在重启服务器,
  2. 根据教程的 source code
  3. 检查我在文件中的代码
  4. 确保 'projects'settings.pyINSTALLED_APPS 列表中。

这是 urls.py 里面的 rp-portfolio\personal_portfolio:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('projects/', include('projects.urls'))
]

这是 rp-portfolio\projects 里面的 urls.py

from django.urls import path
from . import views

urlpatterns = [
    path("", views.project_index, name="project_index"),
    path("<int:pk>/", views.project_detail, name="project_detail"),
]

urls.py

from django.urls import path
from django.views.generic import RedirectView

urlpatterns = [
    path('admin/', admin.site.urls),
    path('projects/', include('projects.urls')),
    path('', RedirectView.as_view(url='/projects/')),
]

试试这个

在urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('projects.urls'))
]

希望对您有所帮助

当您在项目中寻找路径时,您请求的路径必须是这个 http://localhost:8000/projects/

您需要添加:

in polls/urls.py

添加:from django.conf.urls import url

所以它应该看起来像:

from django.urls import path
from django.conf.urls import url
from . import views

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

有时 URL 会从终端获取其他单词。

示例 1.0

http://127.0.0.1:8000/Quit 使用 CTRL-BREAK 的服务器启动开发服务器。

例如在示例 1.0 中,而不是像 http://127.0.0.1:8000/

这样打开 link

link 将是 http://127.0.0.1:8000/Quit,因为 /Quit 未包含在您的代码中,您将收到 404 错误。

示例 2.0

Starting development server at http://127.0.0.1:8000/
Quit the server with 
CTRL-BREAK.
Not Found: /Quit
[27/May/2021 22:36:07] "GET /Quit HTTP/1.1" 404 2066
[27/May/2021 22:36:59] "GET / HTTP/1.1" 200 5873
[27/May/2021 22:37:04] "GET / HTTP/1.1" 200 5873

希望这对您有所帮助。