我已经开始学习Django,并且正在观看this讲座(直到20分钟开始)并按照说明进行操作,但是出现的错误是:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/hello
Using the URLconf defined in lecture3.urls, Django tried these URL patterns, in this order:
admin/
The current path, hello, 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.
运行后
python3 manage.py runserver
“ lecture3”应用中的settings.py文件是:
# Application definition
INSTALLED_APPS = [
'hello',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
和其他一些内容。
“ hello”应用中的views.py文件是:
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return HttpResponse("Hello World!")
“ hello”应用中的urls.py文件是:
from django.urls import path
from . import views
urlpatterns=[
path("",views.index, name="index")
]
“ lecture3”应用中的urls.py文件是:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('/hello/', include('hello.urls'))
]
我在这里检查了类似的问题,但是我的问题没有解决。谁能告诉我为什么我收到此错误。任何帮助,将不胜感激。
答案 0 :(得分:1)
给出:
...
path('/hello/', include('hello.urls'))
...
从路径中删除第一个斜杠:
...
path('hello/', include('hello.urls'))
...
然后,您需要使用/
后面的斜杠http://127.0.0.1:8000/hello/
来访问它
或者按照Django的约定,在您的APPEND_SLASH=True
中使用settings.py
,以便访问http://127.0.0.1:8000/hello
会重定向到http://127.0.0.1:8000/hello/