【问题标题】:Django : GET / HTTP/1.1" 404 2029Django:GET / HTTP/1.1" 404 2029
【发布时间】:2021-11-16 07:16:15
【问题描述】:

我是 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/ 工作正常

【问题讨论】:

  • 您没有在urls.py 中定义的/ 的路径。
  • 当我从 Lecture3/lecture3/urls.py 文件中删除最后一行 path('hello/', include("hello.urls")) 时,127.0.0.1:8000 工作正常

标签: python django


【解决方案1】:

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

  1. 路径('admin/', admin.site.urls)
  2. path('hello/', include("hello.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。因此视图函数将被执行。

【讨论】:

  • 好吧,当我从 Lecture3/lecture3/urls.py 文件中删除最后一行 path('hello/', include("hello.urls")) 时,127.0.0.1:8000 工作正常
  • 你可以修改 lecutre3/lecture3/urls.py 中的 path('', include("hello.urls")。然后如果 URL 像 " localhost:8080****'
  • 我试过了,现在127.0.0.1:8000/hello不行了
  • 这行不通,因为没有该模式的 URL。我建议您再次了解 Django 的基础知识。您可以参考以下频道了解 Django 基础知识。 youtube.com/user/thenewboston
猜你喜欢
  • 2013-03-16
  • 1970-01-01
  • 1970-01-01
  • 2020-03-03
  • 2016-04-11
  • 1970-01-01
  • 1970-01-01
  • 2011-02-15
  • 1970-01-01
相关资源
最近更新 更多