Django网址vi

2024-06-10 19:00:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试实现SayHello应用程序页。这是我的网址.py内容

from django.urls import path
from hello.views import myView
urlpatterns = [
   path('admin/', admin.site.urls),
   path('sayHello/',myView),
]

将此应用程序添加到URL后,http://127.0.0.1:8000/sayHello/工作正常。你知道吗

但是http://127.0.0.1:8000/向我报告以下404错误页。你知道吗

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

admin/
sayHello/
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.

如何修复此错误?你知道吗


Tags: pathdjangoinfromimport应用程序httpurl
3条回答

bcozdjango首先在网址.py然后从视图.py文件。。。 路径('admin/',admin.site.url)将u带到http://127.0.0.1:8000/admin,path('sayHello/',myView)将u带到http://127.0.0.1:8000/sayHello 但是在网址.py你没有匹配到http://127.0.0.1:8000/的url,通常是索引页或主页,,,, 要添加索引,请执行此操作>

urlpatterns = [ 
   path('admin/', admin.site.urls),
   path('sayHello/',myView),
   path('',view),
]

试试这个

from django.urls import path
from hello.views import myView
urlpatterns = [
   path('admin/', admin.site.urls),
   path('sayHello/',myView),
   path('', sayHello),
]

希望有帮助

这是因为您没有与您的主页匹配的路由(主页中是一个空字符串)网址.py例如,如果您这样做,您将在http://127.0.0.1:8000/中看到相同的HelloWorld

from django.urls import path
from hello.views import myView
urlpatterns = [
   path('admin/', admin.site.urls),
   path('',myView),
   path('sayHello/',myView),
]

相关问题 更多 >