gpt4 book ai didi

python - 如何修复 "Page not found (404)"错误 ("Django tried these URL patterns... The empty path didn' t 与其中任何一个匹配。”)

转载 作者:行者123 更新时间:2023-12-01 08:05:55 24 4
gpt4 key购买 nike

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

我正在关注this教程并已完成标题“项目应用程序:模板”的所有内容。现在,当我启动服务器时,地址为 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. 确保'projects'位于settings.py中的INSTALLED_APPS列表内。

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

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/')),
]

关于python - 如何修复 "Page not found (404)"错误 ("Django tried these URL patterns... The empty path didn' t 与其中任何一个匹配。”),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55536646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com