Django:GET / HTTP/1.1" 404 2029

问题描述 投票:0回答:2

我是 Django 初学者。我收到他的 404 错误,找不到原因。一步一步遵循所有说明。使用 Django 版本 3.1.1。试图在谷歌上找到解决方案,但没有帮助。 当我执行

python manage.py runserver 
- 我收到以下消息

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
November 16, 2021 - 06:17:59
Django version 3.1.1, using settings 'lecture3.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /
[16/Nov/2021 06:18:02] "GET / HTTP/1.1" 404 2029
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
November 16, 2021 - 06:25:43
Django version 3.1.1, using settings 'lecture3.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Not Found: /
[16/Nov/2021 06:25:46] "GET / HTTP/1.1" 404 2029

页面上的错误是:

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

admin/
hello/
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
    from django.shortcuts import render

    # Create your views here.
    def index(request):
        return HttpResponse("Hello, world!") 

lecture3/lecture3/urls.py

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

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

http://127.0.0.1:8000/ -> 404 错误 但是

http://127.0.0.1:8000/admin works fine
http://127.0.0.1:8000/hello works fine

请帮忙,需要你的帮助

当我从 Lecture3/lecture3/urls.py 中删除最后一行

path('hello/', include("hello.urls"))
http://127.0.0.1:8000/
工作正常

python django
2个回答
0
投票

我也遇到了同样的问题。请问最后怎么解决?


-2
投票

一旦用户发送 URL 请求,Django 将查看下面提到的匹配项。

  1. 路径('admin/', admin.site.urls)
  2. 路径('你好/',包括(“你好.urls”))

如果您使用“http://127.0.0.1:8000/admin”或“http://127.0.0.1:8000/hello”发出请求,它会起作用,因为 Django 可以找到所请求 URL 的匹配项。

由于“lecture3/lecture3/urls.py”中没有匹配“http://127.0.0.1:8000/”,Django将抛出404。

我相信您会收到“http://127.0.0.1:8000/hello/”的“Hello World”响应,那是因为一旦 django 遇到“http://127.0.0.1:8000/hello”它将包含“hello.urls”。在“hello/urls.py”中,您已将“index”视图附加到“”,空URL。因此视图函数将被执行。

© www.soinside.com 2019 - 2024. All rights reserved.