【发布时间】: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 工作正常